2020 day 7

This commit is contained in:
tristan 2020-12-07 15:17:19 +00:00
parent f3e12ea387
commit 75d5c9f041
4 changed files with 665 additions and 0 deletions

30
2020/07/part2.js Normal file
View file

@ -0,0 +1,30 @@
const fs = require('fs');
const data = fs.readFileSync('input.txt', 'utf-8');
const rules = data.split('\n');
let bagRules = {}
rules.forEach(rule => {
halves = rule.split(' bags contain ')
bagRules[halves[0]] =
halves[1].replace(/( bags| bag|\.)/g, "")
.split(", ")
.map((t)=>{
return {
name: t.replace(/\d /,""),
amt: t[0]
}
})
});
function howManyIn(name) {
let amt = 1
bagRules[name].forEach(rule => {
if (rule.amt == 'n') {return}
amt += parseInt(rule.amt) * howManyIn(rule.name)
})
return amt
}
howManyIn('shiny gold')-1 // subtract the first gold bag