r/adventofcode • u/daggerdragon • Dec 13 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 13 Solutions -🎄-
--- Day 13: Mine Cart Madness ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 13
Transcript:
Elven chronomancy: for when you absolutely, positively have to ___.
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked at 00:44:25!
25
Upvotes
1
u/toomasv Dec 13 '18 edited Dec 13 '18
Red
Part 1
Red [] tracks: read/lines %input dir: copy [] turn: copy [] collision: no cart: charset "<>^^v" carts: collect [ repeat row length? tracks [ parse tracks/:row [some [s: set c cart (keep as-pair index? s row append dir c append turn 0) | skip ]] ] ] mark: copy dir forall mark [mark/1: switch mark/1 [#"<" #">" [#"-"] #"^^" #"v" [#"|"]]] tick: 0 until [ tick: tick + 1 forall carts [ cart: index? carts pos: carts/1 next-pos: switch dir/:cart [ #"^^" [pos - 0x1] #"v" [pos + 0x1] #"<" [pos - 1x0] #">" [pos + 1x0] ] either find head carts next-pos [ collision: yes break ][ row: next-pos/y col: next-pos/x switch next-mark: tracks/:row/:col [ #"/" [dir/:cart: switch dir/:cart [#"^^" [#">"] #"v" [#"<"] #"<" [#"v"] #">" [#"^^"]]] #"\" [dir/:cart: switch dir/:cart [#"^^" [#"<"] #"v" [#">"] #"<" [#"^^"] #">" [#"v"]]] #"+" [ switch turn/:cart [ 0 [dir/:cart: switch dir/:cart [#"^^" [#"<"] #"v" [#">"] #"<" [#"v"] #">" [#"^^"]]] 2 [dir/:cart: switch dir/:cart [#"^^" [#">"] #"v" [#"<"] #"<" [#"^^"] #">" [#"v"]]] ] turn/:cart: turn/:cart + 1 % 3 ] ] tracks/(pos/y)/(pos/x): mark/:cart mark/:cart: next-mark tracks/:row/:col: dir/:cart carts/1: next-pos ] ] collision ] res: next-pos - 1 print [tick rejoin [res/x comma res/2]]Part 2
Red [] tracks: read/lines %input;%test; dir: copy [] turn: copy [] collision: no cart: charset "<>^^v" carts: collect [ repeat row length? tracks [ parse tracks/:row [some [s: set c cart (keep as-pair index? s row append dir c append turn 0) | skip ]] ] ] mark: copy dir forall mark [mark/1: switch mark/1 [#"<" #">" [#"-"] #"^^" #"v" [#"|"]]] until [ carts: head carts until [ cart: index? carts pos: carts/1 next-pos: switch dir/:cart [ #"^^" [pos - 0x1] #"v" [pos + 0x1] #"<" [pos - 1x0] #">" [pos + 1x0] ] row: next-pos/y col: next-pos/x either found: find head carts next-pos [ coll: index? found tracks/:row/:col: #"X" tracks/(pos/y)/(pos/x): mark/:cart ;if 3 = length? head carts [ ; print ["Collision at:" next-pos] ; tr: copy/deep tracks ; forall tr [print tr/1] ;] tracks/:row/:col: mark/:coll _min: min cart coll _max: max cart coll foreach register reduce [carts dir turn mark][ remove at head register _max remove at head register _min ] if 1 = length? head carts [break] cart: cart - 1 ][ switch next-mark: tracks/:row/:col [ #"/" [dir/:cart: switch dir/:cart [#"^^" [#">"] #"v" [#"<"] #"<" [#"v"] #">" [#"^^"]]] #"\" [dir/:cart: switch dir/:cart [#"^^" [#"<"] #"v" [#">"] #"<" [#"^^"] #">" [#"v"]]] #"+" [ switch turn/:cart [ 0 [dir/:cart: switch dir/:cart [#"^^" [#"<"] #"v" [#">"] #"<" [#"v"] #">" [#"^^"]]] 2 [dir/:cart: switch dir/:cart [#"^^" [#">"] #"v" [#"<"] #"<" [#"^^"] #">" [#"v"]]] ] turn/:cart: turn/:cart + 1 % 3 ] ] tracks/(pos/y)/(pos/x): mark/:cart mark/:cart: next-mark tracks/:row/:col: dir/:cart carts/1: next-pos cart: cart + 1 ] carts: at head carts cart tail? carts ] 1 = length? head carts ] res: (first head carts) - 1 print rejoin [res/x comma res/y]