-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.nim
93 lines (75 loc) · 1.87 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
import std/[
algorithm,
bitops,
deques,
intsets,
json,
math,
re,
sequtils,
sets,
strformat,
strutils,
tables,
sugar,
]
import ../../lib/grid
proc parseLine(line: string): (int, int) =
let p = line.split(", ").mapIt(it.parseInt)
(p[0], p[1])
proc parse(input: string): seq[(int, int)] =
input.split("\n").mapIt(it.parseLine)
when defined(test):
let input = """
1, 1
1, 6
8, 3
3, 4
5, 5
8, 9
""".strip
block:
doAssert input.parse == @[(1, 1), (1, 6), (8, 3), (3, 4), (5, 5), (8, 9)]
proc genDistMap(points: seq[(int, int)], size: int): seq[seq[int]] =
result = newSeqWith(size, newSeq[int](size))
for r in 0 ..< size:
for c in 0 ..< size:
var dist = int.high
var target = -1
for i, (x, y) in points:
let d = (r - y).abs + (c - x).abs
if dist > d:
dist = d
target = i
elif dist == d:
target = -1
result[r][c] = target
proc part1(input: string, size = 400): int =
var distMap = genDistMap(input.parse, size)
for r in 0 ..< size:
for c in 0 ..< size:
if r != 0 and r != size - 1 and c != 0 and c != size - 1: continue
if distMap[r][c] == -1: continue
floodfill(distMap, r, c, distMap[r][c], -1)
for r in 0 ..< size:
for c in 0 ..< size:
if distMap[r][c] == -1: continue
result = result.max floodfill(distMap, r, c, distMap[r][c], -1)
when defined(test):
block:
doAssert part1(input, 10) == 17
proc part2(input: string, thres = 10000, size = 400): int =
let points = input.parse
for r in 0 ..< size:
for c in 0 ..< size:
var d = 0
for (x, y) in points:
d += (r - y).abs + (c - x).abs
if d < thres: result += 1
when defined(test):
block:
doAssert part2(input, 32, 10) == 16
when isMainModule and not defined(test):
let input = readAll(stdin).strip
echo part1(input)
echo part2(input)