Repository navigation
Expand file tree
/
Copy path08-2.js
More file actions
58 lines (53 loc) · 1.5 KB
/
Copy path08-2.js
File metadata and controls
58 lines (53 loc) · 1.5 KB
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
const { S_IFREG } = require('constants');
const fs = require('fs');
const { nextTick } = require('process');
fs.readFile('./input', 'UTF-8', (error, data) => {
if (error) {
console.error(error);
return;
}
// Code
var accumulator = 0;
const instructions = data.split('\n');
instructions.pop();
var terminates = false;
for (var i = 0; i < instructions.length; i++) {
const modInstructions = [...instructions];
const instructionToMod = modInstructions[i].split(' ');
if (instructionToMod[0] === 'acc') continue;
else if (instructionToMod[0] === 'jmp') modInstructions[i] = 'nop ' + instructionToMod[1];
else if (instructionToMod[0] === 'nop') modInstructions[i] = 'jmp ' + instructionToMod[1];
accumulator = 0;
var instructionOrder = [];
modInstructions.forEach(() => {
instructionOrder.push(null);
});
var currInstruction = 0;
var step = 0;
while (instructionOrder[currInstruction] === null) {
step++;
instructionOrder[currInstruction] = step;
const instruction = modInstructions[currInstruction].split(' ');
switch (instruction[0]) {
case 'nop':
currInstruction++;
break;
case 'acc':
currInstruction++;
accumulator += parseInt(instruction[1]);
break;
case 'jmp':
currInstruction += parseInt(instruction[1]);
break;
}
if (currInstruction >= modInstructions.length) {
terminates = true;
console.log('terminates');
break;
}
}
if (terminates) break;
}
console.log(terminates);
console.log(accumulator);
});