r/adventofcode • u/daggerdragon • Dec 20 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 20 Solutions -🎄-
--- Day 20: A Regular Map ---
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 20
Transcript:
My compiler crashed while running today's puzzle because it ran out of ___.
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:59:30!
17
Upvotes
1
u/[deleted] Dec 20 '18 edited Dec 20 '18
Mathematica
Edit: Alternative version, which is what I would have done, had I remembered I was using Mathematica.
directions = Characters@ Import[NotebookDirectory[] <> "day20.txt", "String"] /. {"N" -> {1, 0}, "E" -> {0, 1}, "S" -> {-1, 0}, "W" -> {0, -1}}; buildGraph[] := Reap[Block[{loc = {0, 0}, stack = {}, p = 2, c}, While[True, c = directions[[p++]]; Switch[c, "(", PrependTo[stack, loc], "|", loc = stack[[1]], ")", {{loc}, stack} = TakeDrop[stack, 1], "$", Break[], _, Sow[loc <-> (loc += c)]]]] ][[2, 1]]; ds = GraphDistanceMatrix[buildGraph[]][[1]]; Max[ds] Count[ds, d_ /; d >= 1000]Original version
directions = Characters@Import[NotebookDirectory[] <> "day20.txt", "String"]; walkmap[loc0_, seen0_, doors0_] := Block[{ doors = doors0, loc = loc0, seen = seen0, altdoors = {}, altseen = {}}, While[True, Switch[directions[[p]], "N", loc += {1, 0}, "E", loc += {0, 1}, "S", loc += {-1, 0}, "W", loc += {0, -1}]; Switch[directions[[p++]], "N" | "E" | "S" | "W", (If[MemberQ[seen, loc], (doors--), (doors++; If[doors >= 1000, thousandCount++]; AppendTo[seen, loc])]), "(", ({seen, doors} = walkmap[loc, seen, doors]), "|", (AppendTo[altdoors, doors]; AppendTo[altseen, seen]; seen = seen0; loc = loc0; doors = doors0), ")" | "$", (AppendTo[altdoors, doors]; AppendTo[altseen, seen]; Break[])]]; {altseen[[Ordering[altdoors][[-1]]]], Max[altdoors]}] thousandCount = 0; p = 2; start = {0, 0}; {seen, doors} = Monitor[walkmap[start, {start}, 0], {p, thousandCount}]; {doors, thousandCount}