-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.js
53 lines (47 loc) · 1.37 KB
/
day08.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
const fs = require('fs')
function lines (file) {
return fs
.readFileSync(file, 'UTF-8')
.split('\n')
.filter(l => l.length)
}
const memory = {}
let maxValue = 0
function ensureValue (y) {
if (memory[y]) {
return memory[y]
} else {
memory[y] = 0
return 0
}
}
function storeMax(value) {
if (value > maxValue) {
maxValue = value
}
return value
}
function process (lines) {
lines.forEach(l => {
const [x, op, dx, iff, y, cmp, yv] = l.split(' ')
const truthy = (() => {
switch (cmp) {
case '>=': return ensureValue(y) >= parseInt(yv)
case '<=': return ensureValue(y) <= parseInt(yv)
case '<': return ensureValue(y) < parseInt(yv)
case '>': return ensureValue(y) > parseInt(yv)
case '==': return ensureValue(y) === parseInt(yv)
case '!=': return ensureValue(y) !== parseInt(yv)
}})()
if (truthy) {
memory[x] = op === 'inc'
? storeMax(ensureValue(x) + parseInt(dx))
: storeMax(ensureValue(x) - parseInt(dx))
}
})
return memory
}
const processedMemory = process(lines('./day8-input.txt'))
const values = Object.keys(processedMemory).map(k => processedMemory[k])
console.log(Math.max(...values))
console.log(maxValue)