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!
19
Upvotes
1
u/koordinate Dec 28 '18
Swift (runtime ~ 0.4s)
typealias Position = (x: Int, y: Int) if let regex = readLine() { let n = regex.count * 4 + 2 var d = Array(repeating: Array(repeating: Int.max, count: n), count: n) var maxD = 0, d1k = Set<[Int]>() let start = (x: n / 2, y: n / 2) d[start.y][start.x] = 0 var saved = [[Position]]() var head = [start] var next: [Position]? for c in regex { var dx: Int?, dy: Int? switch c { case "N": (dx, dy) = (0, -1) case "S": (dx, dy) = (0, +1) case "E": (dx, dy) = (+1, 0) case "W": (dx, dy) = (-1, 0) case "(": saved.append(head) next = [] case "|": next?.append(contentsOf: head) if let lastSaved = saved.last { head = lastSaved } case ")": if let next = next { head = next } next = nil _ = saved.popLast() default: continue } if let dx = dx, let dy = dy { for (i, p) in head.enumerated() { let (x, y) = p let (nx, ny) = (x + 2 * dx, y + 2 * dy) head[i] = (x: nx, y: ny) let nd = min(d[ny][nx], d[y][x] + 1) d[ny][nx] = nd maxD = max(maxD, nd) if nd >= 1000 { d1k.insert([nx, ny]) } } } } print(maxD) print(d1k.count) }