summaryrefslogtreecommitdiffstats
path: root/day14/day14-1.js
diff options
context:
space:
mode:
Diffstat (limited to 'day14/day14-1.js')
-rw-r--r--day14/day14-1.js19
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))