-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.js
80 lines (65 loc) · 2.22 KB
/
14.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const util = require('./util.js')
const TODAY = 14
const MASK = 'mask'
function main() {
const ops = util.getInputFromFile(TODAY)
.split('\n')
.map(e=>e.split(' = '))
console.log("Part 1: " + part1(ops))
console.log("Part 2: " + part2(ops))
}
function part1(ops) {
let maskArr = [], abs=new Map()
for (op of ops) {
if (op[0] === MASK) {
maskArr = op[1].split('')
}
else {
let targetAddress = op[0].match(/[0-9]+/)[0]
let memValueBinArr = parseInt(op[1])
.toString(2) // Convert to binary
.padStart(36, '0') // Pad string with zeroes
.split('') // Convert to array
let res = [...maskArr]
.map((e, index) => e === 'X' ? memValueBinArr[index] : e) // If 'X' then replace else keep Mask value
abs.set(targetAddress, parseInt(res.join(''), 2)) // Overwrite Memory
}
}
return [...abs.values()].reduce((a, e) => a+=e, 0) // Sum up all memory values
}
function part2(ops) {
let maskArr = []
const abs=new Map()
for (op of ops) {
if (op[0] === MASK) {
maskArr = op[1].split('')
}
else {
const memory = parseInt(op[0]
.match(/[0-9]+/)[0])
.toString(2)
.padStart(36, '0')
.split('')
const value = parseInt(op[1])
let indices = []
const res = [...maskArr].map((e, index) => {
if (e === 'X') {
indices.push(index)
} else if(e === '0') {
return memory[index]
}
return e
})
for(j = 0; j < 2**indices.length; j++) {
const bin = j.toString(2).padStart(indices.length, '0').split('')
let res_tmp = [...res]
bin.forEach((e, i) => {
res_tmp[indices[i]] = e
})
abs.set(parseInt(res_tmp.join(''),2), value)
}
}
}
return [...abs.values()].reduce((a,e) => a += e)
}
main();