-
Notifications
You must be signed in to change notification settings - Fork 1
/
a07.nim
101 lines (77 loc) · 2.41 KB
/
a07.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
import strutils, sequtils, strscans, sugar, algorithm, tables
# --- Node class ---
type GenericNode[T] = ref object
value:T
parents: seq[GenericNode[T]]
children: seq[GenericNode[T]]
proc newNode[T](value:T):GenericNode[T]=
result = new(GenericNode[T])
result.value = value
# --- Graph class ---
type Node = GenericNode[char]
type Graph = seq[Node]
proc getOrAdd(g:var Graph, value:char):Node =
let gf = g.filterIt(it.value == value)
if gf.len == 0:
result = newNode(value)
g.add(result)
elif gf.len == 1:
result = gf[0]
proc remove(g:var Graph, n:Node) =
for np in n.parents:
np.children = np.children.filterIt(it != n)
for nc in n.children:
nc.parents = nc.parents.filterIt(it != n)
let index = g.find(n)
if index >= 0: g.del(index)
proc remove(g:var Graph, value:char) =
for index, n in g:
if n.value == value:
g.remove(n)
proc connect(g:var Graph, a,b:Node) =
a.children.add(b)
b.parents.add(a)
proc connect(g:var Graph, a,b:char) =
var na = g.getOrAdd(a)
var nb = g.getOrAdd(b)
g.connect(na,nb)
# --- Read data ---
proc getGraph():Graph=
result = newSeq[Node]()
for line in lines("7_input.txt"):
var a,b:string
discard line.scanf("Step $w must be finished before step $w can begin.", a,b)
result.connect(a[0],b[0])
# --- Part 1 ---
var graph = getGraph()
var part1:string
while true:
let orphans = graph.filterIt(it.parents.len == 0).sorted(cmp)
if(orphans.len == 0): break
part1 &= orphans[0].value
graph.remove(orphans[0])
echo "Part 1: ", part1
# --- Part 2 ---
graph = getGraph()
var workers = newTable[char, int]()
var step = 0
while true:
# decrease all worker time and remove if zero
for k in workers.keys:
dec(workers[k])
if workers[k] <= 0:
workers.del(k)
graph.remove(k)
# find all possible nodes, e.g. those with no parents and not already worked on
var orphans = graph.filterIt(it.parents.len == 0)
.filterIt(not workers.hasKey(it.value))
.sorted(cmp)
# start new workers
while workers.len < 5 and orphans.len > 0:
let value = orphans[0].value
let time = 61 + orphans[0].value.int - 'A'.int
workers.add(value, time)
orphans.delete(0,0)
if graph.len == 0: break
inc(step)
echo "Part 2: ", step