r/adventofcode • u/daggerdragon • Dec 23 '22
SOLUTION MEGATHREAD -π- 2022 Day 23 Solutions -π-
All of our rules, FAQs, resources, etc. are in our community wiki.
UPDATES
[Update @ 00:21:46]: SILVER CAP, GOLD 68
- Stardew Valley ain't got nothing on these speedy farmer Elves!
AoC Community Fun 2022:
πΏπ MisTILtoe Elf-ucation π§βπ«
- Submissions are CLOSED!
- Thank you to all who submitted something!
- Every last one of you are awesome!
- Community voting is OPEN!
- 42 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 23: Unstable Diffusion ---
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:24:43, megathread unlocked!
20
Upvotes
View all comments
2
u/Key__Strokes Dec 23 '22 edited Dec 25 '22
Javascript
Solution to both parts
Util method: addBuffer - Add buffer to the input grid
Part 1:
gridCreate a
directionsmap which has pairs that define movements in row and column fashion.We also create an array of
movementConsiderations, which define what movement order the elves consider for round 1 for the proposal. It references thedirectionsmap using the key belowREF1: We run the following steps 10 times
addBuffermethodmovementGrid, which records where an elf wants to moveIterate on the input grid to determine each elf's movement preference
Iterate on row of grid: row = 0 -> Max Rows - 1
Iterate on col of grid: col = 0 -> Max Columns - 1
If the current grid element is #, then:
movementConsiderationsarray, find which preference will work for the elf. Add the first direction of this preference to themovementGridarrayIterate on
movementGridarray to determine if there are clashes in two or more Elves for a spot.Lets create a list of Elves interested in a spot. Create a new 2D array called
claimantsIterate on row of
movementGrid: row = 0 -> Max Rows - 1Iterate on col of
movementGrid: col = 0 -> Max Columns - 1movementGridfor the current row and column, and if there is a claimant for this spot, then add the claimant's row and column position toclaimantsarrayclaimants, and move Elves if there is only 1 claimant for a spotmovementConsiderationsarray, so that the top preference becomes the least preferred optionLets determine the number of land in the smallest rectangle
Part 2:
counterand assign it to 0In REF1, we run the loop infinite times
If you liked the explanation, then please don't forget to cast your vote π to
Adventures of Advent of Code - Edition 1 - /u/Key__Strokesin the poll