fn calc_joltage(
values: &[u32],
count: usize,
cache: &mut HashMap<(usize, usize), usize>,
) -> usize {
if let Some(result) = cache.get(&(values.len(), count)) {
return *result;
}
if count == 0 {
return 0;
}
let mut highest = 0;
let mut highest_base = 0;
for (i, value) in values[0..values.len() - count + 1].iter().enumerate() {
if *value < highest_base {
continue;
}
let base_joltage = (*value as usize) * 10_usize.pow(count as u32 - 1);
let joltage = base_joltage + calc_joltage(&values[i + 1..], count - 1, cache);
if joltage > highest {
highest = joltage;
highest_base = *value;
}
}
cache.insert((values.len(), count), highest);
highest
}
#[test]
fn test_y2025_day3_part2() {
let input = std::fs::read_to_string("input/2025/day_3.txt").unwrap();
let mut total = 0;
input.lines().for_each(|line| {
let banks = line
.chars()
.map(|c| c.to_digit(10).unwrap())
.collect::<Vec<u32>>();
let joltage = calc_joltage(&banks, 12, &mut HashMap::new());
total += joltage;
});
println!("Total: {}", total);
}
Seems i missed the faster solutions, but i did get this down to a respectable 400ms. edit: 400ms was not respectable, mykl's method took 1ms. Mine was close though, with a bit more brain and optimisation I got there.
And the bot worked all by itself!