2018 d7 p2
This commit is contained in:
parent
de804608ef
commit
2e77fcf631
38
2018/7/part2.js
Normal file
38
2018/7/part2.js
Normal file
|
@ -0,0 +1,38 @@
|
|||
const fs = require('fs');
|
||||
let rules = fs.readFileSync('input.txt', 'utf-8').split('\n');
|
||||
steps = {};
|
||||
for (let rule of rules) {
|
||||
[_, step1, step2] = rule.match(/Step (\w) must be finished before step (\w) can begin./);
|
||||
if (!steps[step1])
|
||||
steps[step1] = []
|
||||
if (!steps[step2])
|
||||
steps[step2] = []
|
||||
steps[step2].push(step1)
|
||||
}
|
||||
|
||||
let elves = {};
|
||||
for (let time = 0; Object.entries(steps).length > 0; time++) {
|
||||
if (elves[time]) {
|
||||
console.log(`step ${elves[time]} done`)
|
||||
remove(elves[time])
|
||||
delete elves[time];
|
||||
}
|
||||
let avaliable = Object.entries(steps).filter(v => v[1].length == 0).map(v => v[0]).sort();
|
||||
for (let i = 0; Object.entries(elves).length < 5 && i < avaliable.length; i++) {
|
||||
if (!Object.values(elves).some(v => v == avaliable[i])) {
|
||||
elves[time + timeof(avaliable[i])] = avaliable[i]
|
||||
}
|
||||
}
|
||||
console.log(time, Object.values(elves))
|
||||
}
|
||||
|
||||
function timeof(step) {
|
||||
return 60 + '0ABCDEFGHIJKLMNOPQRSTUVWXYZ'.indexOf(step)
|
||||
}
|
||||
|
||||
function remove(step) {
|
||||
delete steps[step];
|
||||
for (let s in steps) {
|
||||
steps[s] = steps[s].filter(v => v != step)
|
||||
}
|
||||
}
|
|
@ -22,4 +22,4 @@
|
|||
1. [:star: :star:](https://adventofcode.com/2018/day/4 "see puzzle")
|
||||
1. [:star: :star:](https://adventofcode.com/2018/day/5 "see puzzle")
|
||||
1. [:star: :star:](https://adventofcode.com/2018/day/6 "see puzzle")
|
||||
1. [:star:](https://adventofcode.com/2018/day/7 "see puzzle")
|
||||
1. [:star: :star:](https://adventofcode.com/2018/day/7 "see puzzle")
|
||||
|
|
|
@ -23,9 +23,9 @@ if (advent) {
|
|||
1. [js :star: :star:](https://adventofcode.com/2015/day/4 "see puzzle")
|
||||
1. [js :star: :star:](https://adventofcode.com/2015/day/5 "see puzzle")
|
||||
1. [js :star: :star:](https://adventofcode.com/2015/day/6 "see puzzle")
|
||||
1. [js :star:](https://adventofcode.com/2015/day/7 "see puzzle")
|
||||
1. [js :star: :star:](https://adventofcode.com/2015/day/7 "see puzzle")
|
||||
1. [-](https://adventofcode.com/2015/day/8 "see puzzle")
|
||||
|
||||
## Languages Used
|
||||
* Python 8×:star:
|
||||
* Node JS 13×:star:
|
||||
* Node JS 14×:star:
|
Loading…
Reference in a new issue