diff options
author | 2020-12-10 00:24:00 +0100 | |
---|---|---|
committer | 2020-12-10 00:24:00 +0100 | |
commit | c0617b6edbae172752b58276a7678e8c97ade3e7 (patch) | |
tree | beb6bc9d8016be1da2a2a6d9977151714fcee569 /day7/day7-1.js | |
parent | cbbdf1c07d629d2947659768110ccf2a6dc5066b (diff) | |
download | advent_of_code_2020-c0617b6edbae172752b58276a7678e8c97ade3e7.tar.gz advent_of_code_2020-c0617b6edbae172752b58276a7678e8c97ade3e7.tar.bz2 advent_of_code_2020-c0617b6edbae172752b58276a7678e8c97ade3e7.zip |
day7
Diffstat (limited to 'day7/day7-1.js')
-rw-r--r-- | day7/day7-1.js | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/day7/day7-1.js b/day7/day7-1.js new file mode 100644 index 0000000..a28222a --- /dev/null +++ b/day7/day7-1.js @@ -0,0 +1,48 @@ +const data = require('./data') + +let 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 null + }})]) + .map(s => [s[0], s[1].filter(x => x !== null)]) + +const keys = arrays.map(s => s[0]) +const check = (arr, k) => ( + arr.find(a => { + let c = false + a[1].forEach(x => { + if (x[0] !== 'shiny gold' && k.indexOf(x[0]) > -1) { c = true } + }) + return c + }) +) + +while ( + check(arrays, keys) +) { + arrays = arrays.map(s => [s[0], s[1].map(x => { + if (keys.indexOf(x[0]) > -1 && x[0] !== 'shiny gold') { + return arrays.find(a => a[0] == x[0])[1].map(y => [y[0], y[1] * x[1]]) + } else { + return [x] + } + })]) + arrays = arrays.map(a => [a[0], a[1].flat(1)]) +} + +let res = 0 +arrays.forEach(a => { + if (a[1] && a[1].length) { res += 1 } +}) + +console.log('result: ', res) + |