blob: 68006d6604a2fc55637e92edc79659bf431ae7f1 (
plain) (
blame)
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
|
const { _, performance } = require("perf_hooks");
const fs = require("fs");
const data = fs.readFileSync("input", "utf8").split('\n');
function run(code)
{
let acc = 0;
let visited = [];
let i = 0;
while (!visited.includes(i)) {
visited.push(i);
if (!code[i])
return { acc, i };
const [op, cnt] = code[i].split(" ");;
switch (op) {
case "acc":
acc += +cnt;
break;
case "jmp":
i += +cnt - 1;
break;
default:
break;
}
i++;
}
return { acc, i };
}
function partOne()
{
return run(data).acc;
}
function partTwo()
{
for (let i = 0; i < data.length; i++) {
const [op, cnt] = data[i].split(" ");;
if (op == "acc")
continue; // Skip correction
let fixed = [...data];
fixed.splice(i, 1, `${op == "nop" ? "jmp" : "nop"} ${cnt}`);
let results = run(fixed);
if (results.i == fixed.length)
return results.acc;
}
}
const tic = performance.now();
console.log(partOne());
console.log(partTwo());
const toc = performance.now();
console.log("TIME: " + ((toc - tic) / 1000).toFixed(6) + " seconds");
|