2018 d7 p2

This commit is contained in:
Tristan 2018-12-08 12:17:25 +00:00
parent de804608ef
commit 2e77fcf631
3 changed files with 41 additions and 3 deletions

38
2018/7/part2.js Normal file
View 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)
}
}

View file

@ -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")

View file

@ -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&times;:star:
* Node JS 13&times;:star:
* Node JS 14&times;:star: