-
Notifications
You must be signed in to change notification settings - Fork 0
/
day07.js
72 lines (62 loc) · 1.69 KB
/
day07.js
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
// 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.
// `
fs = require('fs')
input = fs.readFileSync('inputs/07.txt', 'utf-8')
lines = input.trim().split('\n')
re = /^Step ([A-Z]).+step ([A-Z])/
steps = {}
function init(step) {
if (!steps[step]) {
steps[step] = { parents: [], children: [] }
}
}
lines.forEach(line => {
let [_, parent, child] = line.match(re)
init(parent); init(child)
steps[parent].children.push(child)
steps[child].parents.push(parent)
})
const available = []
for (let step in steps) {
if (!steps[step].parents.length) {
available.push(step)
}
}
let order = ''
let seconds = 0
const workers = 5
const baseTaskLength = 60
const working = new Set()
function pickUpWork() {
while (available.length && working.size < workers) {
const nextName = available.sort().shift()
const step = steps[nextName]
step.doneAt = baseTaskLength + nextName.charCodeAt() - 64 + seconds
working.add(nextName)
}
}
while (available.length || working.size) {
pickUpWork()
working.forEach(nextName => {
if (seconds == steps[nextName].doneAt) {
working.delete(nextName)
order += nextName
steps[nextName].children.forEach(child => {
if (steps[child].parents.every(p => order.includes(p))) {
available.push(child)
}
})
pickUpWork()
}
})
seconds++
}
console.log(order)
console.log(seconds - 1)