-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution_2018_13.swift
216 lines (186 loc) · 5.91 KB
/
Solution_2018_13.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import Foundation
private enum Track: String, Equatable {
case straight = "|"
case side = "-"
case curveRight = "/"
case curveLeft = "\\"
case intersection = "+"
case empty = " "
init(_ string: String) {
if let cartDirection = Cart.Direction(rawValue: string) {
// we know that carts stand on straight lines only
switch cartDirection {
case .up, .down:
self = .straight
case .left, .right:
self = .side
}
} else {
self.init(rawValue: string)!
}
}
}
private struct Position: Equatable, Comparable, Hashable {
var x: Int
var y: Int
static func < (lhs: Position, rhs: Position) -> Bool {
return lhs.y == rhs.y ? lhs.x < rhs.x : lhs.y < rhs.y
}
}
private struct Cart {
enum Direction: String, CaseIterable {
case up = "^"
case right = ">"
case down = "v"
case left = "<"
mutating func cw() {
let directions = Direction.allCases
self = directions[(directions.firstIndex(of: self)! + 1) % directions.count]
}
mutating func ccw() {
let directions = Direction.allCases
self = directions[(directions.firstIndex(of: self)! + directions.count - 1) % directions.count]
}
}
enum IntersectionOrder: CaseIterable {
case left
case straight
case right
mutating func next() {
let cases = IntersectionOrder.allCases
self = cases[(cases.firstIndex(of: self)! + 1) % cases.count]
}
}
var position: Position
var direction: Direction
var nextIntersection = IntersectionOrder.left
var crashed: Bool = false
mutating func intersection() {
switch nextIntersection {
case .straight:
//no-op
break
case .left:
direction.ccw()
case .right:
direction.cw()
}
nextIntersection.next()
}
mutating func corner(with track: Track) {
if Set([.up, .down]).contains(direction) {
track == .curveLeft
? direction.ccw()
: direction.cw()
} else {
track == .curveLeft
? direction.cw()
: direction.ccw()
}
}
mutating func drive() {
switch direction {
case .up:
position.y -= 1
case .down:
position.y += 1
case .left:
position.x -= 1
case .right:
position.x += 1
}
}
init(position: Position, direction: Direction) {
self.position = position
self.direction = direction
}
}
private func print(map: [[Track]], carts: [Cart]) {
map.indices.forEach({ i in
map[i].indices.forEach({ j in
if let cart = carts.first(where: { $0.position == Position(x: j, y: i) }) {
print(cart.direction.rawValue, terminator:"")
} else {
print(map[i][j].rawValue, terminator:"")
}
})
print()
})
}
struct Solution_2018_13: Solution {
var input: Input
func run() throws {
//let input = """
///->-\\
//| | /----\\
//| /-+--+-\\ |
//| | | | v |
//\\-+-/ \\-+--/
//\\------/
//"""
//.components(separatedBy: .newlines)
let input = try self.input.get(trimmingWhitespace: false)
.components(separatedBy: .newlines)
let map = input.map({ $0.map({ Track(String($0)) }) })
var carts = [Position: Cart]()
for i in input.indices {
for (j, elem) in input[i].enumerated() {
if let direction = Cart.Direction(rawValue: String(elem)) {
let pos = Position(x: j, y: i)
carts[pos] = Cart(position: pos, direction: direction)
}
}
}
// returns first crash location if it happened
func tick(_ map: [[Track]], _ carts: inout [Position: Cart]) -> Position? {
// print(map: map, carts: Array(carts.values))
func move(_ cart: inout Cart) {
cart.drive()
let track = map[cart.position.y][cart.position.x]
switch track {
case .straight, .side:
//no-op
break
case .curveLeft, .curveRight:
cart.corner(with: track)
case .intersection:
cart.intersection()
case .empty:
fatalError("Please don't leave the track!")
}
}
let cartPositions = carts.keys.sorted()
var crashedPosition: Position?
for position in cartPositions {
// we might have removed a cart on this tick already
guard carts[position] != nil else { continue }
move(&carts[position]!)
let newPosition = carts[position]!.position
if carts[newPosition] != nil {
carts[newPosition] = nil
carts[position] = nil
crashedPosition = newPosition
} else {
carts[newPosition] = carts[position]
carts[position] = nil
}
}
return crashedPosition
}
var crash: Position?
while crash == nil {
crash = tick(map, &carts)
}
let part1 = "\(crash!.x),\(crash!.y)"
print(part1)
while carts.count != 1 {
_ = tick(map, &carts)
}
let lastCart = carts.first!.value
let part2 = "\(lastCart.position.x),\(lastCart.position.y)"
print(part2)
// ------- Test -------
assert(part1 == "26,99", "WA")
assert(part2 == "62,48", "WA")
}
}