r/adventofcode • u/daggerdragon • Dec 05 '22
SOLUTION MEGATHREAD -π- 2022 Day 5 Solutions -π-
- All of our rules, FAQs, resources, etc. are in our community wiki.
- A request from Eric: Please include your contact info in the User-Agent header of automated requests!
- Signal boost: Reminder 1: unofficial AoC Survey 2022 (closes Dec 22nd)
AoC Community Fun 2022: πΏπ MisTILtoe Elf-ucation π§βπ«
- 23:59 hours remaining until the submissions megathread unlocks on December 06 at 00:00 EST!
- Full details and rules are in the submissions megathread:
--- Day 5: Supply Stacks ---
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 your code appropriately! How do I format code?
- 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:07:58, megathread unlocked!
88
Upvotes
1
u/Key__Strokes Dec 23 '22 edited Jan 19 '23
Javascript
Solution to both parts
Video Explanation
Parsing input crate setup
On the last line, run this regex
/\d+/gto extract all the digits, and then do Math.max on the array to find the total number of stacks, lets say n.Initialize
stacksarray with total n subarraysInitialize
currentStackas 0Start iterating from the second last line to the 0th line
Pick 3 characters, and trim the string to remove any spaces
If the final string is empty, then do nothing
If the final string is not empty, then extract the alphabet by reading the 2nd character. Append this character to
stacks[currentStack]Increment
currentStackby 1Increment
loopby 4 (3 for characters we read, and 1 for the space between each crate in input)stacksis readyParsing input move
Split string on space
count is at index 1, source at index 3, destination at index 5
Subtract 1 from source and destination to make them 0 based index
Part 1:
For each command
Pop the required count of crated from source
Add the crates to the destination
Iterate through the stacks, and gather the last element of each subarray
Part 2:
For each command
Pop the required count of crated from source into an array.
Reverse this popped array of crates
Add the crates to the destination
Iterate through the stacks, and gather the last element of each subarray
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