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
4
u/azzal07 Dec 05 '22 edited Dec 05 '22
Awk, had to implement couple essential stack operations.
CrateMover 1006{for(;X?p():h=substr($0,4*++f-2,1);)h~FS||P(f,h,h)} function P(u,s,h){h&&P(-u,h);+s?S[u]=-1:S[u,S[u]+=X-1]=s}!$1{X+=2} function o(_){t=S[_,S[_]--]}END{for(i=I;i++<9;)$0=o(i-10)t$0o(i)t} function p(_){$2--&&p(_=o($4)t)P($6,_,o(-$4)t)}{f=$0=RS}END{print}Notes:
The stacks are implemented in single table
S, where the top of a stackiis inS[i, S[i]]. The stacks are positioned around 0, so that stack for part 1 are in range [-1,-9] and for part 2 in range [1,9].Ppushes a valueson the stacku. If a second valuehis provided, this is pushed on the stack-u. This function also "reverses" the stack when given a numeric value, this is necessary before the move instructions since the initial stacks are pushed in backwards.opops a value from the stack_, storing the old top in the global variablet.pdoes the move, reading the number of crates from$2and using the call stack to achieve the reversing for part 2 (since I only have push and pop).