-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.nim
129 lines (107 loc) · 2.81 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import std/[
algorithm,
bitops,
deques,
heapqueue,
intsets,
json,
math,
re,
sequtils,
sets,
strformat,
strutils,
tables,
sugar,
]
proc parseLine(line: string): (string, string) =
if line =~ re"Step (\w+) must be finished before step (\w+) can begin.":
(matches[0], matches[1])
else:
raise newException(ValueError, "parse error: " & line)
proc parse(input: string): Table[string, seq[string]] =
let lines = input.split("\n")
var names = initHashSet[string]()
for line in lines:
let (a, b) = line.parseLine
names.incl a
names.incl b
for name in names:
result[name] = newSeq[string]()
for line in lines:
let (a, b) = line.parseLine
result[a].add b
when defined(test):
let input = """
Step C must be finished before step A can begin.
Step C must be finished before step F can begin.
Step A must be finished before step B can begin.
Step A must be finished before step D can begin.
Step B must be finished before step E can begin.
Step D must be finished before step E can begin.
Step F must be finished before step E can begin.
""".strip
block:
doAssert input.parse == {
"A": @["B", "D"],
"B": @["E"],
"C": @["A", "F"],
"D": @["E"],
"E": @[],
"F": @["E"]
}.toTable
proc part1(input: string): string =
let next = input.parse
var prev = initTable[string, HashSet[string]]()
for u in next.keys: prev[u] = initHashSet[string]()
for u, vs in next:
for v in vs:
prev[v].incl u
var cands = initHeapQueue[string]()
for k, v in prev:
if v.len == 0:
cands.push k
while cands.len > 0:
let c = cands.pop
result &= c
for v in next[c]:
prev[v].excl c
if prev[v].len == 0:
cands.push v
when defined(test):
block:
doAssert part1(input) == "CABDFE"
proc part2(input: string, numWorkers, cap: int): int =
let next = input.parse
var prev = initTable[string, HashSet[string]]()
for u in next.keys: prev[u] = initHashSet[string]()
for u, vs in next:
for v in vs:
prev[v].incl u
var cands = initHeapQueue[string]()
for k, v in prev:
if v.len == 0:
cands.push k
proc workTime(s: string): int =
s[0].ord - 'A'.ord + 1 + cap
var workers = initHeapQueue[(int, string)]()
var t = 0
while cands.len > 0 or workers.len > 0:
if cands.len == 0 or workers.len == numWorkers:
let (nextAvail, completed) = workers.pop
t = t.max nextAvail
for v in next[completed]:
prev[v].excl completed
if prev[v].len == 0:
cands.push v
if cands.len > 0:
let task = cands.pop
workers.push (t + task.workTime, task)
t
when defined(test):
block:
doAssert part2(input, 2, 0) == 15
when isMainModule and not defined(test):
let input = readAll(stdin).strip
echo part1(input)
echo part2(input, 5, 60)