aoc/2020/07/part2.js

30 lines
640 B
JavaScript
Raw Permalink Normal View History

2020-12-07 15:17:19 +00:00
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