diff --git a/2018/7/part2.js b/2018/7/part2.js new file mode 100644 index 0000000..600a9be --- /dev/null +++ b/2018/7/part2.js @@ -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) + } +} diff --git a/2018/README.md b/2018/README.md index 654d1ed..4f73a68 100644 --- a/2018/README.md +++ b/2018/README.md @@ -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") diff --git a/README.md b/README.md index 97af4c0..3b4b7b4 100644 --- a/README.md +++ b/README.md @@ -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: \ No newline at end of file +* Node JS 14×:star: \ No newline at end of file