-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.js
116 lines (96 loc) · 2.89 KB
/
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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const fs = require('fs')
fs.readFile('./input_day8.txt', 'utf8' , (err, data) => {
if (err) {
console.error(err)
return err;
}
//console.log(calculateAccumulator(data));
fixCode(data);
})
function fixCode(input) {
let instructions = input.split("\n");
console.log("Instructions", instructions.length);
//instructions.splice(instructions.length - 1, 1);
let origInstructions = instructions;
//instructions[instructions.length-1] = "end"
console.log(instructions[instructions.length-1]);
let nopIndices = [];
let jmpIndices = [];
for (var i = 0; i < instructions.length; i++) {
if (instructions[i].split(" ")[0] === "nop") {
nopIndices.push(i);
} else if (instructions[i].split(" ")[0] === "jmp") {
jmpIndices.push(i);
}
}
console.log("NOPs", nopIndices.length);
console.log("JMPs", jmpIndices.length);
console.log(nopIndices);
let nop = 0;
let jmp = 0;
let [success, acc, max] = calculateAccumulator(instructions);
console.log(success, acc, max);
let tmp = "";
for (var i = 0; i < nopIndices.length; i++) {
//instructions = origInstructions;
tmp = instructions[nopIndices[i]];
instructions[nopIndices[i]] = instructions[nopIndices[i]].replace("nop", "jmp");
[success, acc, max] = calculateAccumulator(instructions);
console.log(success, acc, max);
instructions[nopIndices[i]] = tmp;
}
for (var i = 0; i < jmpIndices.length; i++) {
//instructions = origInstructions;
tmp = instructions[jmpIndices[i]];
instructions[jmpIndices[i]] = instructions[jmpIndices[i]].replace("jmp", "nop");
[success, acc, max] = calculateAccumulator(instructions);
console.log(success, acc, max);
instructions[jmpIndices[i]] = tmp;
}
}
function calculateAccumulator(instructions) {
let acc = 0;
let success = false;
//instructions[instructions.length-1] = "end";
// setup array to track instruction runs
let instrRun = [];
for (var i = 0; i < instructions.length; i++) {
instrRun[i] = 0;
}
let max = 0;
let step = 0;
i = 0;
while (instrRun[i] === 0) {
max = Math.max(i,max);
//console.log(instructions[i]);
if (instructions[i] === "" || instructions[i] === undefined) {
console.log("END");
success = true;
break;
}
instrRun[i] = 1;
let action = instructions[i].split(" ")[0];
let value = instructions[i].split(" ")[1];
if (value.includes("+")) {
step = parseInt(value.split("+")[1]);
} else {
step = (-1)*parseInt(value.split("-")[1]);
}
//console.log(i, action, step);
if (action === "acc") {
acc += step;
} else if (action === "jmp") {
i += step;
}
if (action !== "jmp") {
i++;
}
if (i >= instructions.length) {
console.log("overflow");
i = 0;
}
}
//console.log(i, instructions[i]);
//console.log("Acc:", acc, "Sucecss:", success);
return [success, acc, max];
}