diff options
Diffstat (limited to 'day7/day7-2.js')
-rw-r--r-- | day7/day7-2.js | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/day7/day7-2.js b/day7/day7-2.js new file mode 100644 index 0000000..64f6b5b --- /dev/null +++ b/day7/day7-2.js @@ -0,0 +1,37 @@ +const data = require('./data') +const arrays = data + .replace(/\n/g,"") + .replace(/bag(s)?/g,"") + .split('.').slice(0,-1) + .map(s => s.split(' contain ')) + .map(s => [s[0].trim(), s.slice(1).map(x => x.trim().split(' , '))[0]]) + .map(s => [s[0], s[1].map(x => { + if (x !== 'no other') { + return[x.replace(/\d+/g, '').trim(), parseInt(x.match(/[0-9]+/g))] + } else { + return ['no other', 1] + }})]) + .map(s => [s[0], s[1].filter(x => x !== null)]) + +let gold = arrays.find(s => s[0] === 'shiny gold')[1] +let parents = 0 + +gold.forEach(g => parents += g[1]) + +while ( + gold.map(g => g[0] !== 'no other').some(g => true) +) { + gold = gold.map(g => { + if (arrays.find(s => s[0] === g[0])) { + return arrays.find(s => s[0] === g[0])[1].map(x => { + if (x[0] != 'no other') { + parents = parents + (x[1] * g[1]) + } + return ([x[0], x[1] * g[1]]) + }) + } else { return [] } + }).flat(1) +} + +console.log(parents) + |