diff options
author | 2020-12-14 12:10:49 +0100 | |
---|---|---|
committer | 2020-12-14 12:10:49 +0100 | |
commit | 2f7c1f10323a26dea066eecb52b1c3fae746c0f5 (patch) | |
tree | 88dc34b4df1178d6c04726ac8aa75dc28edd5dc9 /day14/day14-1.js | |
parent | a1cb1a1b16b2304ee2c688e289395c65e4d7a657 (diff) | |
download | advent_of_code_2020-2f7c1f10323a26dea066eecb52b1c3fae746c0f5.tar.gz advent_of_code_2020-2f7c1f10323a26dea066eecb52b1c3fae746c0f5.tar.bz2 advent_of_code_2020-2f7c1f10323a26dea066eecb52b1c3fae746c0f5.zip |
day14
Diffstat (limited to 'day14/day14-1.js')
-rw-r--r-- | day14/day14-1.js | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/day14/day14-1.js b/day14/day14-1.js new file mode 100644 index 0000000..6e72909 --- /dev/null +++ b/day14/day14-1.js @@ -0,0 +1,19 @@ +const data = require('./data') +const splitted = data + .split(/mask = /g) + .filter(d => d !== '\n') + .map(d => d.split(/\n/g)) + .map(d => d.map((a, i) => (i > 0 ? a.match(/\d+/g) : a)).filter(a => a)) + .map(d => d.map((a, i) => (i > 0 ? a.map((b, i) => i > 0 ? parseInt(b).toString(2).padStart(36, '0') : parseInt(b)) : a))) + +let mem = [] +splitted.forEach(s => { + const mask = s[0] + const applyMask = (str) => ( + str.split('').map((v, i) => (mask[i] !== 'X' ? mask[i] : v)).join('') + ) + + s.forEach((m, i) => (i > 0 && (mem[m[0]] = parseInt(applyMask(m[1]), 2)))) +}) + +console.log('Answer: ', mem.reduce((a, b) => a + b, 0)) |