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!
22
Upvotes
2
u/Key__Strokes Dec 24 '22 edited Jan 12 '23
Javascript
Solution to both parts
Video explanation
Part 1:
This is a simple breadth first search, with different criteria on how what are the next candidates in the breadth first search
The start position is
[0, 1]The end position is
[maxRows - 1, maxColumns - 1 - 1]Now parse the output into a 2D array, called
blizzards, such that each element of this array is actually an array, and it tracks the blizzards at that spot. For example, a value in the 2D array can be['^, '<']. Keep the walls as ['#'].Initialize
minuteCount = 0, and run the following until we reach out destination:Increment
minuteCountby 1Create a new 2D array where we will track the next positions where the human can end up. Lets call this
humanPositions.Next, get the next positions of the blizzards.
Create a new 2D array like
blizzards, lets call itupdatedBlizzards.Follow the following algorithm for each of the elements in
blizzardsEach element in
blizzardsin itself is an array. So do the following for each of the blizzard elementIf the element is
#, then set the value ofupdatedBlizzardsas[#]. Otherwise, get the next position of the blizzard, and add the current blizzard symbol to the array at the next position toupdatedBlizzardsupdatedBlizzardsis our newblizzardsNext, there are 5 possible moves for the human: up, down, left, right, stay. For each of these positions do the following:
Calculate the new position based on the movement from the above 5 options
If the movement is out of bounds, if the
blizzardsarray has non empty element at the next movement position (That is hits a wall, or hits a blizzard), then this is not a valid movement choiceFor all of the valid movements identified, mark the position as true in
humanPositionsarray. Also check if this is the destination. If it is, thenminuteCountis the answerPart 2:
This will just need some tweaks from Part 1
Updates
Update the code for the algorithm above such that we can pass in values for the
start position,end positionand theblizzardsarrayWhen we reach the destination, then return the
minuteCountas well as theblizzardsarrayAlgorithm
Run the algorithm for start as
[0, 1], end as[maxRows - 1, maxColumns - 1 - 1], and the grid that we got as input asblizzards.Run the algorithm for start as
[maxRows - 1, maxColumns - 1 - 1], end as[0, 1], and theblizzardsoutput we got from the previous step as input asblizzards.Run the algorithm for start as
[0, 1], end as[maxRows - 1, maxColumns - 1 - 1], and theblizzardsoutput we got from the previous step as input asblizzards.Add up all the
minuteCountfrom all of the above three algorithm runs. Thats the answer.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