-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8.js
35 lines (32 loc) · 908 Bytes
/
day8.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
const fs = require('fs');
const input = fs.readFileSync('./day8_input.txt', { encoding: 'utf8' })
.trim()
.split('\n')
.map(line => line.trim().split(/\s+/))
function part1() {
const myInput = input.slice(0);
const record = {};
let max = -Infinity;
for (const line of myInput) {
record[line[0]] = record[line[0]] || 0;
record[line[4]] = record[line[4]] || 0;
if (run(record[line[4]], line[5], line[6])) {
if (line[1] == 'inc') {
record[line[0]] += Number(line[2]);
} else if (line[1] == 'dec') {
record[line[0]] -= Number(line[2]);
}
if (record[line[0]] > max) {
max = record[line[0]];
}
}
}
process.nextTick(() => {
console.log('Part 2: %d', max);
})
return Math.max.apply(null, Object.values(record));
function run(a, op, b) {
return eval(`${a} ${op} ${b}`);
}
}
console.log('Part 1: %d', part1());