-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.nim
89 lines (74 loc) · 1.42 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
import ../../lib/imports
type
Tree = Table[string, HashSet[string]]
proc parse(input: string): Tree =
result = initTable[string, HashSet[string]]()
for line in input.split("\n"):
let p = line.split(")")
let (a, b) = (p[0], p[1])
if a notin result:
result[a] = initHashSet[string]()
result[a].incl b
if b notin result:
result[b] = initHashSet[string]()
result[b].incl a
proc depthSum(tree: Tree): int =
proc dfs(u, p: string, depth: int): int =
result += depth
for v in tree[u]:
if v == p: continue
result += dfs(v, u, depth + 1)
dfs("COM", "", 0)
when defined(test):
let input = """
COM)B
B)C
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L
""".strip
block:
doAssert input.parse.depthSum == 42
proc part1(input: string): int =
input.parse.depthSum
proc dist(tree: Tree, src, dst: string): int =
var res = 0
proc dfs(u, p: string, d: int): bool =
if dst in tree[u]:
res = d
return true
for v in tree[u]:
if v == p: continue
if dfs(v, u, d + 1): return true
discard dfs(src, "", 0)
res - 1
when defined(test):
let input1 = """
COM)B
B)C
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L
K)YOU
I)SAN
""".strip
block:
doAssert input1.parse.dist("YOU", "SAN") == 4
proc part2(input: string): int =
input.parse.dist("YOU", "SAN")
when isMainModule and not defined(test):
let input = readFile("input").strip
echo part1(input)
echo part2(input)