this post was submitted on 11 Dec 2025
16 points (100.0% liked)

Advent Of Code

1223 readers
2 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 11: Reactor

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
[–] EnEnCode@programming.dev 1 points 2 months ago

Rust

Somehow got the right answer for part 1 iteratively, but it wasn't actually correct at all, so switched to the standard recursion + memoisation. Gotta love just chucking an iterator into the value type of a HashMap and going BRRRR (1.2 ms).

solution

pub fn day11(input: &str) -> (u64, u64) {
    let array = input.trim().lines();
    let mut conns = HashMap::new();
    for line in array {
        let mut iter = line.split_whitespace();
        conns.insert(&iter.next().unwrap()[0..3], iter);
    }

    fn find_path<'a>(
        start: &'a str,
        end: &'a str,
        conns: &HashMap<&'a str, SplitWhitespace<'a>>,
        devices: &mut HashMap<&'a str, u64>,
    ) -> u64 {
        let mut sum = 0;
        let Some(list) = conns.get(start) else {
            return 0;
        };
        for i in list.clone() {
            if i == end {
                sum += 1;
            } else if let Some(precomp) = devices.get(i) {
                sum += precomp;
            } else {
                sum += find_path(i, end, conns, devices);
            }
        }
        devices.insert(start, sum);
        sum
    }

    let part1 = find_path("you", "out", &conns, &mut HashMap::new());
    // If dac -> fft and fft -> dac are both non-zero, then there are loops. That makes an
    // infinite number of paths so the question posed would be meaningless.
    let dac_to_fft = find_path("dac", "fft", &conns, &mut HashMap::new());
    let part2 = if dac_to_fft == 0 {
        find_path("svr", "fft", &conns, &mut HashMap::new())
            * find_path("fft", "dac", &conns, &mut HashMap::new())
            * find_path("dac", "out", &conns, &mut HashMap::new())
    } else {
        find_path("svr", "dac", &conns, &mut HashMap::new())
            * dac_to_fft
            * find_path("fft", "out", &conns, &mut HashMap::new())
    };
    (part1, part2)
}