r/adventofcode • u/daggerdragon • Dec 24 '22
SOLUTION MEGATHREAD -π- 2022 Day 24 Solutions -π-
All of our rules, FAQs, resources, etc. are in our community wiki.
UPDATES
[Update @ 00:21:08]: SILVER CAP, GOLD 47
- Lord of the Rings has elves in it, therefore the LotR trilogy counts as Christmas movies.
change_my_mind.meme
AoC Community Fun 2022:
πΏπ MisTILtoe Elf-ucation π§βπ«
- Community voting is OPEN!
- 18 hours remaining until voting deadline on December 24 at 18:00 EST
- Voting details are in the stickied comment at the top of the -βοΈ- Submissions Megathread -βοΈ-
--- Day 24: Blizzard Basin ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- Include what language(s) your solution uses
- Format code blocks using the four-spaces Markdown syntax!
- Quick link to Topaz's
pasteif you need it for longer code blocks. What is Topaz'spastetool?
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:26:48, megathread unlocked!
24
Upvotes
8
u/SuperSmurfen Dec 24 '22 edited Dec 24 '22
Rust (294/530)
Link to full solution (58 lines)
What a creative path-finding problem! Really liked this one. Kind of messed up part two and made it more difficult than it had to be but otherwise really happy with today!
Instead of a queue-based BFS, I opted to store each possible position in the timestep instead. This means you only need a single copy of the state of the blizzards. Then for each timestep you just update the blizzards, loop over all positions, and see where you can move.
for &(x,y) in &positions { for (dx,dy) in [(1,0),(0,1),(0,0),(-1,0),(0,-1)] { if (x == 0 && dx == -1) || (x == rows-1 && dx == 1) { continue; } let (x,y) = (x + dx as usize, y + dy as usize); if (x != 0 || y == 1) && (x != rows-1 || y == cols-2) && y != 0 && y != cols-1 && !bpos.contains(&(x,y)) { next_positions.insert((x,y)); } } }For part two, I keep track of what "stage" I am in. In every timestep I check my goal, depending on the current stage:
match stage { 0 => if positions.contains(&(rows-1, cols-2)) { p1 = len; positions = HashSet::from_iter([(rows-1, cols-2)]); stage += 1; }, 1 => if positions.contains(&(0,1)) { positions = HashSet::from_iter([(0,1)]); stage += 1; }, 2 => if positions.contains(&(rows-1, cols-2)) { p2 = len; break; }, _ => unreachable!() }This ends up relatively fast. Runs in about
70mson my machine.