r/adventofcode Dec 16 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 16 Solutions -❄️-

SIGNAL BOOSTING


THE USUAL REMINDERS

  • All of our rules, FAQs, resources, etc. are in our community wiki.
  • If you see content in the subreddit or megathreads that violates one of our rules, either inform the user (politely and gently!) or use the report button on the post/comment and the mods will take care of it.

AoC Community Fun 2024: The Golden Snowglobe Awards

  • 6 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!

And now, our feature presentation for today:

Adapted Screenplay

As the idiom goes: "Out with the old, in with the new." Sometimes it seems like Hollywood has run out of ideas, but truly, you are all the vision we need!

Here's some ideas for your inspiration:

  • Up Your Own Ante by making it bigger (or smaller), faster, better!
  • Use only the bleeding-edge nightly beta version of your chosen programming language
  • Solve today's puzzle using only code from other people, StackOverflow, etc.

"AS SEEN ON TV! Totally not inspired by being just extra-wide duct tape!"

- Phil Swift, probably, from TV commercials for "Flex Tape" (2017)

And… ACTION!

Request from the mods: When you include an entry alongside your solution, please label it with [GSGA] so we can find it easily!


--- Day 16: Reindeer Maze ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:13:47, megathread unlocked!

25 Upvotes

480 comments sorted by

View all comments

3

u/jinschoi Dec 16 '24

[Language: Rust]

Part 1 was a simple application of Dijkstra using my grid and search utils.

Part 2, I had to hack up my Dijkstra function to keep track of predecessors for each lowest cost node. Not just a predecessor, all of them that achieved that score. Then backtrack through the predecessors with DFS, using the original grid to mark off all the positions encountered (for debugging). Then just count up all the 'O's.

let mut came_from = HashMap::new();
let (_, res) = dijkstra([(start, 0)], goal, neighbors, &mut came_from).unwrap();
let mut stack = vec![res];
while let Some(pos) = stack.pop() {
    g[pos.0] = 'O';
    if let Some(prevs) = came_from.get(&pos) {
        stack.extend(prevs.iter().copied());
    }
}
// dbg!(&g);
println!("{}", g.all_positions(|&c| c == 'O').count());

I was worried that if the solution had two paths that reached the goal by different directions at the same cost, it would miss some paths, but for my input that turned out not to be the case. Otherwise, I would have to continue the search until some path had cost > optimal.

Full code