Published
Edited
Nov 13, 2019
Insert cell
Insert cell
Insert cell
Insert cell
work = {
const result = []
const queue = _.difference(nodes, _.map(dependencies, 1))
let edges = [...dependencies]
let workers = []
const time = part => 60 + part.charCodeAt(0) - 64
function getWork() {
while (workers.length < 5 && queue.length) {
const part = queue.shift()
workers.push({ part, remaining: time(part), i: workers.length })
}
}
getWork()
let t = 0
while (workers.length) {
// pass time
const done = workers.filter(worker => {
worker.remaining--
return worker.remaining <= 0
})

// finish work
for (const { part } of done) {
const [ resolved, remaining ] = _.partition(edges, d => d[0] === part)
const next = _.difference(_.map(resolved, 1), _.map(edges = remaining, 1))
queue.push(...next)
}
// pick up more work
if (done.length) {
workers = _.difference(workers, done)
getWork()
}
// tick
t++

for (const worker of workers) {
result.push({ t, worker: {...worker} })
}
}
return result
}
Insert cell
part1 = {
const result = []
const queue = _.difference(nodes, _.map(dependencies, 1))
let edges = [...dependencies]
let visit = null
while (visit = queue.sort().shift()) {
result.push(visit)
const [ resolved, remaining ] = _.partition(edges, d => d[0] === visit)
const next = _.difference(_.map(resolved, 1), _.map(edges = remaining, 1))
queue.push(...next)
}
return result.join('')
}
Insert cell
Insert cell
Insert cell
viewof debug = html`<input type="checkbox" />`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more