-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
348 lines (323 loc) · 5.92 KB
/
main.go
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package main
import (
"io"
aoc "github.com/teivah/advent-of-code"
)
type tileType int
const (
none tileType = iota
vertical
horizontal
bendL
bendJ
bend7
bendF
ground
startingPosition
inside
)
func fs1(input io.Reader) int {
lines := aoc.ReaderToStrings(input)
board, start := toBoard(lines)
next, dir := getNextPosition(board, start)
distance := 1
for next != start {
c := board[next]
switch c {
case horizontal:
switch dir {
case aoc.Left, aoc.Right:
next = next.Move(dir, 1)
default:
panic(dir)
}
case vertical:
switch dir {
case aoc.Up, aoc.Down:
next = next.Move(dir, 1)
default:
panic(dir)
}
case bendL:
switch dir {
case aoc.Down:
dir = aoc.Right
case aoc.Left:
dir = aoc.Up
default:
panic(dir)
}
next = next.Move(dir, 1)
case bendJ:
switch dir {
case aoc.Right:
dir = aoc.Up
case aoc.Down:
dir = aoc.Left
default:
panic(dir)
}
next = next.Move(dir, 1)
case bend7:
switch dir {
case aoc.Right:
dir = aoc.Down
case aoc.Up:
dir = aoc.Left
default:
panic(dir)
}
next = next.Move(dir, 1)
case bendF:
switch dir {
case aoc.Up:
dir = aoc.Right
case aoc.Left:
dir = aoc.Down
default:
panic(dir)
}
next = next.Move(dir, 1)
default:
panic(c)
}
distance++
}
return (distance + 1) / 2
}
func toBoard(lines []string) (map[aoc.Position]tileType, aoc.Position) {
board := make(map[aoc.Position]tileType)
var start aoc.Position
for row, line := range lines {
for col, c := range line {
pos := aoc.Position{Row: row, Col: col}
switch c {
case '|':
board[pos] = vertical
case '-':
board[pos] = horizontal
case 'L':
board[pos] = bendL
case 'J':
board[pos] = bendJ
case '7':
board[pos] = bend7
case 'F':
board[pos] = bendF
case '.':
board[pos] = ground
case 'S':
board[pos] = startingPosition
start = pos
default:
panic(string(c))
}
}
}
return board, start
}
func getNextPosition(board map[aoc.Position]tileType, start aoc.Position) (aoc.Position, aoc.Direction) {
var (
tileUp = board[start.Delta(-1, 0)]
tileDown = board[start.Delta(1, 0)]
tileLeft = board[start.Delta(0, -1)]
tileRight = board[start.Delta(0, 1)]
)
switch tileUp {
case bend7, bendF, vertical:
return start.Delta(-1, 0), aoc.Up
}
switch tileDown {
case bendL, bendJ, vertical:
return start.Delta(1, 0), aoc.Down
}
switch tileLeft {
case bendL, bendF, horizontal:
return start.Delta(0, -1), aoc.Left
}
switch tileRight {
case bendJ, bend7, horizontal:
return start.Delta(0, 1), aoc.Right
}
panic("direction")
}
func fs2(input io.Reader) int {
lines := aoc.ReaderToStrings(input)
board, start := toBoard(lines)
loopPositions := make(map[aoc.Position]bool)
loopPositions[start] = true
next, dir := getNextPosition(board, start)
for next != start {
loopPositions[next] = true
c := board[next]
switch c {
case horizontal:
switch dir {
case aoc.Left, aoc.Right:
next = next.Move(dir, 1)
default:
panic(dir)
}
case vertical:
switch dir {
case aoc.Up, aoc.Down:
next = next.Move(dir, 1)
default:
panic(dir)
}
case bendL:
switch dir {
case aoc.Down:
dir = aoc.Right
case aoc.Left:
dir = aoc.Up
default:
panic(dir)
}
next = next.Move(dir, 1)
case bendJ:
switch dir {
case aoc.Right:
dir = aoc.Up
case aoc.Down:
dir = aoc.Left
default:
panic(dir)
}
next = next.Move(dir, 1)
case bend7:
switch dir {
case aoc.Right:
dir = aoc.Down
case aoc.Up:
dir = aoc.Left
default:
panic(dir)
}
next = next.Move(dir, 1)
case bendF:
switch dir {
case aoc.Up:
dir = aoc.Right
case aoc.Left:
dir = aoc.Down
default:
panic(dir)
}
next = next.Move(dir, 1)
default:
panic(c)
}
}
replaceStart(board, start)
count := 0
for row := 0; row < len(lines); row++ {
for col := 0; col < len(lines[0]); col++ {
pos := aoc.Position{Row: row, Col: col}
if isInsideHorizontal(board, loopPositions, pos, aoc.Right) {
board[pos] = inside
count++
}
}
}
return count
}
func replaceStart(board map[aoc.Position]tileType, start aoc.Position) {
var (
tileUp = board[start.Delta(-1, 0)]
tileDown = board[start.Delta(1, 0)]
tileLeft = board[start.Delta(0, -1)]
tileRight = board[start.Delta(0, 1)]
up = false
down = false
left = false
right = false
)
switch tileUp {
case bend7, bendF, vertical:
up = true
}
switch tileDown {
case bendL, bendJ, vertical:
down = true
}
switch tileLeft {
case bendL, bendF, horizontal:
left = true
}
switch tileRight {
case bendJ, bend7, horizontal:
right = true
}
var t tileType
if up {
if down {
t = vertical
} else if left {
t = bendJ
} else if right {
t = bendL
}
} else if down {
if up {
t = vertical
} else if left {
t = bend7
} else if right {
t = bendF
}
} else if left {
if up {
t = bendJ
} else if down {
t = bend7
} else if right {
t = horizontal
}
}
if t == none {
panic(start)
}
board[start] = t
}
func isInsideHorizontal(board map[aoc.Position]tileType, loopPositions map[aoc.Position]bool, pos aoc.Position, dir aoc.Direction) bool {
if loopPositions[pos] {
return false
}
count := 0
latestFigure := none
for {
pos = pos.Move(dir, 1)
if board[pos] == none {
return count%2 != 0
}
if loopPositions[pos] {
switch board[pos] {
case bendL, bendF, bendJ, bend7:
if latestFigure == none {
count++
latestFigure = board[pos]
} else {
switch latestFigure {
case bendL:
// A 7 cancels an L
if board[pos] != bend7 {
count++
}
case bendF:
// A J cancel an F
if board[pos] != bendJ {
count++
}
}
latestFigure = none
}
case vertical:
count++
case horizontal:
default:
panic(pos)
}
}
}
}