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
[โ€“] [S] 1 point 9 months ago* (last edited 9 months 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!

  • source