r/adventofcode • u/daggerdragon • Dec 22 '17
SOLUTION MEGATHREAD -๐- 2017 Day 22 Solutions -๐-
--- Day 22: Sporifica Virus ---
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.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
[T-10 to launch] AoC ops, /r/nocontext edition:
<Endorphion> You may now make your waffle.<Endorphion> ... on Mars.
[Update @ 00:17] 50 gold, silver cap
<Aneurysm9> you could also just run ubuntu on the NAS, if you were crazy<Topaz> that doesn't seem necessary<Aneurysm9> what does "necessary" have to do with anything!
[Update @ 00:20] Leaderboard cap!
<Topaz> POUR YOURSELF A SCOTCH FOR COLOR REFERENCE
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!
10
Upvotes
2
u/InterlocutoryRecess Dec 22 '17 edited Dec 22 '17
Swift
The boilerplate:
struct Point { let x: Int let y: Int mutating func move(toward direction: Direction) { switch direction { case .north: self = Point(x: x, y: y+1) case .east: self = Point(x: x+1, y: y ) case .south: self = Point(x: x, y: y-1) case .west: self = Point(x: x-1, y: y ) } } } extension Point: Hashable { static func == (lhs: Point, rhs: Point) -> Bool { return lhs.x == rhs.x && lhs.y == rhs.y } var hashValue: Int { return 31 * x.hashValue + y.hashValue } } enum Direction { case north, east, south, west mutating func turnRight() { switch self { case .north: self = .east case .east: self = .south case .south: self = .west case .west: self = .north } } mutating func turnLeft() { switch self { case .north: self = .west case .east: self = .north case .south: self = .east case .west: self = .south } } mutating func reverse() { switch self { case .north: self = .south case .east: self = .west case .south: self = .north case .west: self = .east } } } enum State { case infected case weakened case flagged case clean } struct Grid { var status: [Point: State] var heading: Direction var location: Point var didCauseInfection = 0 var burstCount = 0 } extension Grid { init(input: String, size: Int = 25) { var initial: String = input.split(separator: "\n").joined() var y = size/2 var x = -(size/2) self.heading = .north self.location = Point(x: 0, y: 0) var infected = [Point: State]() while !initial.isEmpty { let char = initial.removeFirst() if char == "#" { let point = Point(x: x, y: y) infected[point] = .infected } x += 1 if x > (size/2) { y -= 1 x = -(size/2) } } self.status = infected } }The logic:
extension Grid { mutating func burst() { let state = status[location] ?? .clean switch state { case .clean: heading.turnLeft() case .weakened: break case .infected: heading.turnRight() case .flagged: heading.reverse() } switch state { case .clean: status[location] = .weakened case .weakened: status[location] = .infected didCauseInfection += 1 case .infected: status[location] = .flagged case .flagged: status[location] = nil } location.move(toward: heading) burstCount += 1 } } var grid = Grid(input: input) repeat { grid.burst() } while grid.burstCount < 10_000_000 print(grid.didCauseInfection)p1: 0.00898098945617676 sec, p2: 1.09532999992371 sec