-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.nim
77 lines (64 loc) · 1.77 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
import std/[
algorithm,
bitops,
re,
sequtils,
sets,
strformat,
strutils,
tables,
]
proc parseLine(line: string): (string, string, int) =
if line =~ re"(\w+) to (\w+) = (\d+)":
(matches[0], matches[1], matches[2].parseInt)
else:
raise newException(ValueError, "parse error: " & line)
when defined(test):
doAssert parseLine("London to Dublin = 464") == ("London", "Dublin", 464)
proc parse(input: string): seq[seq[int]] =
var mapping = initTable[string, int]()
proc getMapping(s: string): int =
if s notin mapping: mapping[s] = mapping.len
mapping[s]
var mapped = newSeq[(int, int, int)]()
for line in input.split("\n"):
let (a, b, w) = parseLine(line)
let u = getMapping(a)
let v = getMapping(b)
mapped.add (u, v, w)
let N = mapping.len
var adj = newSeqWith(N, newSeq[int](N))
for i in 0 ..< N: adj[i].fill int.high
for (u, v, w) in mapped:
adj[u][v] = w
adj[v][u] = w
adj
proc dist(route: var seq[int], adj: var seq[seq[int]]): int =
for i in 1 ..< route.len:
let u = route[i - 1]
let v = route[i]
if adj[u][v] != int.high:
result += adj[u][v]
proc minDist(adj: var seq[seq[int]]): int =
let N = adj.len
result = int.high
var route = (0 ..< N).toSeq
while true:
result = result.min dist(route, adj)
if not route.nextPermutation: break
proc part1(input: string): int =
var adj = input.parse
minDist(adj)
proc maxDist(adj: var seq[seq[int]]): int =
let N = adj.len
var route = (0 ..< N).toSeq
while true:
result = result.max dist(route, adj)
if not route.nextPermutation: break
proc part2(input: string): int =
var adj = input.parse
maxDist(adj)
when isMainModule and not defined(test):
let input = readAll(stdin).strip
echo part1(input)
echo part2(input)