From 2f7c1f10323a26dea066eecb52b1c3fae746c0f5 Mon Sep 17 00:00:00 2001 From: Piotr Russ Date: Mon, 14 Dec 2020 12:10:49 +0100 Subject: day14 --- day14/day14-1.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 day14/day14-1.js (limited to 'day14/day14-1.js') 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)) -- cgit v1.2.3