-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.js
45 lines (39 loc) · 1.49 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
const entry = require('./entries/day08.json');
const ACCUMULATOR = 'acc';
const JUMP = 'jmp';
const NO_OPERATION = 'nop';
const code = entry.map(line => {
const [operation, argument] = line.split(/\s/);
return {
operation,
argument: parseInt(argument),
};
});
const compute = (instructions, { depth = 0, acc = 0, visited = [], canMutate = null } = {}) => {
if (depth === instructions.length) return acc;
if (visited.includes(depth)) return canMutate !== null ? null : acc;
const { operation, argument } = instructions[depth];
visited = [...visited, depth];
depth = depth + 1;
let potentialNextExec = null;
switch (operation) {
case ACCUMULATOR:
potentialNextExec = compute(instructions, { depth, acc: acc + argument, visited, canMutate });
break;
case JUMP:
potentialNextExec = compute(instructions, { depth: depth + argument - 1, acc, visited, canMutate });
if (potentialNextExec === null && !!canMutate) {
potentialNextExec = compute(instructions, { depth, acc, visited, canMutate: false });
}
break;
case NO_OPERATION:
potentialNextExec = compute(instructions, { depth, acc, visited, canMutate });
if (potentialNextExec === null && !!canMutate) {
potentialNextExec = compute(instructions, { depth: depth + argument - 1, acc, visited, canMutate: false });
}
break;
}
return potentialNextExec;
};
console.log(compute(code)); // PART 1
console.log(compute(code, { canMutate: true })); // PART 2