this post was submitted on 03 Dec 2025
26 points (100.0% liked)

Advent Of Code

1199 readers
15 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2025

Solution Threads

M T W T F S S
1 2 3 4 5 6 7
8 9 10 11 12

Visualisations Megathread

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 2 years ago
MODERATORS
 

Day 3: Lobby

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] CameronDev@programming.dev 1 points 3 weeks ago* (last edited 3 weeks ago)
   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!