-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.nim
263 lines (236 loc) · 5.17 KB
/
solution.nim
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
import ../../lib/imports
const EMPTY = '.'
type
Grid = seq[string]
proc parse(input: string): Grid =
input.split("\n")
proc getAngle(dy, dx: int): float =
let g = gcd(dx, dy)
arctan2((dy div g).float, (dx div g).float)
proc scan(g: Grid, r0, c0: int): Table[float, seq[(int, int)]] =
let (rows, cols) = (g.len, g[0].len)
result = initTable[float, seq[(int, int)]]()
for r in 0 ..< rows:
for c in 0 ..< cols:
if (r, c) == (r0, c0): continue
if g[r][c] == EMPTY: continue
let angle = getAngle(r - r0, c - c0)
if angle notin result:
result[angle] = newSeq[(int, int)]()
result[angle].add (r, c)
proc countLines(g: Grid, r, c: int): int =
g.scan(r, c).len
when defined(test):
block:
let input = """
.#..#
.....
#####
....#
...##
""".strip
let g = input.parse
let res = """
.7..7
.....
67775
....7
...87
""".strip.split("\n")
for r in 0 ..< g.len:
for c in 0 ..< g[0].len:
if g[r][c] == EMPTY: continue
doAssert g.countLines(r, c) == res[r][c].ord - '0'.ord
proc findTarget(g: Grid): (int, (int, int)) =
var (tr, tc) = (-1, -1)
var maxLines = 0
for r in 0 ..< g.len:
for c in 0 ..< g[0].len:
if g[r][c] == EMPTY: continue
let lines = g.countLines(r, c)
if maxLines < lines:
maxLines = lines
(tr, tc) = (r, c)
(maxLines, (tr, tc))
proc part1(input: string): int =
let g = input.parse
let (maxLines, _) = g.findTarget
maxLines
when defined(test):
block:
let input = """
......#.#.
#..#.#....
..#######.
.#.#.###..
.#..#.....
..#....#.#
#..#....#.
.##.#..###
##...#..#.
.#....####
""".strip
doAssert input.parse.findTarget == (33, (8, 5))
block:
let input = """
#.#...#.#.
.###....#.
.#....#...
##.#.#.#.#
....#.#.#.
.##..###.#
..#...##..
..##....##
......#...
.####.###.
""".strip
doAssert input.parse.findTarget == (35, (2, 1))
block:
let input = """
.#..#..###
####.###.#
....###.#.
..###.##.#
##.##.#.#.
....###..#
..#.#..#.#
#..#.#.###
.##...##.#
.....#.#..
""".strip
doAssert input.parse.findTarget == (41, (3, 6))
let input = """
.#..##.###...#######
##.############..##.
.#.######.########.#
.###.#######.####.#.
#####.##.#.##.###.##
..#####..#.#########
####################
#.####....###.#.#.##
##.#################
#####.##.###..####..
..######..##.#######
####.##.####...##..#
.#####..#.######.###
##...#.##########...
#.##########.#######
.####.#.###.###.#.##
....##.##.###..#####
.#.#.###########.###
#.#.#.#####.####.###
###.##.####.##.#..##
""".strip
block:
doAssert input.parse.findTarget == (210, (13, 11))
proc dist(r1, c1, r2, c2: int): float =
let dr = (r1 - r2).float
let dc = (c1 - c2).float
(dr * dr + dc * dc).sqrt
proc vaporList(g: Grid): seq[(int, int)] =
let (_, (tr, tc)) = g.findTarget
let scans = g.scan(tr, tc)
var angles = scans.keys.toSeq.sorted
let i = angles.lowerBound(-PI / 2)
angles = angles[i .. ^1] & angles[0 ..< i]
var sortedScans = initTable[float, seq[(int, int)]]()
for a in angles:
sortedScans[a] = scans[a].sortedByIt(-dist(tr, tc, it[0], it[1]))
while true:
var hasMore = false
for a in angles:
if sortedScans[a].len == 0: continue
hasMore = true
result.add sortedScans[a].pop
if not hasMore: break
when defined(test):
block:
let input = """
.#....#####...#..
##...##.#####..##
##...#...#.#####.
..#.....X...###..
..#.#.....#....##
""".strip
let vl = input.parse.vaporList
#[
00000000001111111
01234567890123456
-------------------
0|.#....###24...#..
1|##...##.13#67..9#
2|##...#...5.8####.
3|..#.....X...###..
4|..#.#.....#....##
]#
doAssert vl[0 .. 8] == @[
(1, 8), (0, 9), (1, 9),
(0, 10), (2, 9), (1, 11),
(1, 12), (2, 11), (1, 15)
]
#[
00000000001111111
01234567890123456
-------------------
0|.#....###.....#..
1|##...##...#.....#
2|##...#......1234.
3|..#.....X...5##..
4|..#.9.....8....76
]#
doAssert vl[9 .. 17] == @[
(2, 12), (2, 13), (2, 14),
(2, 15), (3, 12), (4, 16),
(4, 15), (4, 10), (4, 4)
]
#[
00000000001111111
01234567890123456
-------------------
0|.8....###.....#..
1|56...9#...#.....#
2|34...7...........
3|..2.....X....##..
4|..1..............
]#
doAssert vl[18 .. 26] == @[
(4, 2), (3, 2), (2, 0),
(2, 1), (1, 0), (1, 1),
(2, 5), (0, 1), (1, 5)
]
#[
00000000001111111
01234567890123456
-------------------
0|......234.....6..
1|......1...5.....7
2|.................
3|........X....89..
4|.................
]#
doAssert vl[27 .. 35] == @[
(1, 6), (0, 6), (0, 7),
(0, 8), (1, 10), (0, 14),
(1, 16), (3, 13), (3, 14)
]
block:
let vl = input.parse.vaporList
doAssert vl[0] == (12, 11)
doAssert vl[1] == (1, 12)
doAssert vl[2] == (2, 12)
doAssert vl[9] == (8, 12)
doAssert vl[19] == (0, 16)
doAssert vl[49] == (9, 16)
doAssert vl[99] == (16, 10)
doAssert vl[198] == (6, 9)
doAssert vl[199] == (2, 8)
doAssert vl[200] == (9, 10)
doAssert vl[298] == (1, 11)
proc part2(input: string): int =
let vl = input.parse.vaporList
let (r, c) = vl[199]
c * 100 + r
when isMainModule and not defined(test):
let input = readFile("input").strip
echo part1(input)
echo part2(input)