|
@@ -27,29 +27,24 @@ func nearest(coords: seq[Point], point: Point): int =
|
27
|
27
|
else:
|
28
|
28
|
-1
|
29
|
29
|
|
30
|
|
-func inrange(coords: seq[Point], point: Point): bool = coords.map(proc (coord: Point): int =
|
31
|
|
- coord.distance(point)).sum < 10000
|
|
30
|
+func inrange(coords: seq[Point], point: Point): bool =
|
|
31
|
+ coords.map(proc (coord: Point): int = coord.distance(point)).sum < 10000
|
32
|
32
|
|
33
|
33
|
func part1(coords: seq[Point], extents: array[4, int]): int =
|
34
|
|
- var
|
35
|
|
- counts = newSeqWith(coords.len, 0)
|
36
|
|
- excluded = newSeqWith(coords.len, false)
|
|
34
|
+ var counts = newSeqWith(coords.len, 0)
|
37
|
35
|
|
38
|
36
|
for x in extents[0] .. extents[1]:
|
39
|
37
|
for y in extents[2] .. extents[3]:
|
40
|
38
|
let coord = coords.nearest([x, y])
|
41
|
|
- if coord != -1:
|
|
39
|
+ if coord != -1 and counts[coord] > -1:
|
42
|
40
|
counts[coord].inc
|
43
|
41
|
|
44
|
42
|
# If the area reaches the edge of the grid, it will continue infinitely.
|
45
|
43
|
# Mark that area as excluded.
|
46
|
44
|
if x == extents[0] or x == extents[1] or y == extents[2] or y == extents[3]:
|
47
|
|
- excluded[coord] = true
|
|
45
|
+ counts[coord] = -1
|
48
|
46
|
|
49
|
|
- zip(counts, excluded)
|
50
|
|
- .filter(proc(x: tuple[a: int, b: bool]): bool = not x.b)
|
51
|
|
- .map(proc(x: tuple[a: int, b: bool]): int = x.a)
|
52
|
|
- .max
|
|
47
|
+ counts.max
|
53
|
48
|
|
54
|
49
|
func part2(coords: seq[Point], extents: array[4, int]): int =
|
55
|
50
|
var
|
|
@@ -68,7 +63,7 @@ func part2(coords: seq[Point], extents: array[4, int]): int =
|
68
|
63
|
min_y = int.high
|
69
|
64
|
max_y = int.high
|
70
|
65
|
|
71
|
|
- for y in extents[2] - extension .. extents[3] + extension:
|
|
66
|
+ for y in y_start .. y_finish:
|
72
|
67
|
if coords.inrange([x,y]):
|
73
|
68
|
if min_y == int.high:
|
74
|
69
|
min_y = y
|