-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpart-two.js
More file actions
45 lines (38 loc) · 1.41 KB
/
part-two.js
File metadata and controls
45 lines (38 loc) · 1.41 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
const colors = require('colors');
const { program } = require('./input');
const DeviceInstructions = require('./device-instructions');
let instruction_pointer = program.shift();
instruction_pointer = instruction_pointer[1];
let device = new DeviceInstructions(program);
device.setInputPointer(instruction_pointer);
let stops = {};
let stops_length = 0;
let num_of_executions = 0;
// Each time we find a value that'll halt the program, store it.
// The first time we see a value we've seen before, the stop before
// that duplicate is our answer
let last_stop_seen = -1;
// Line 28 is first line where we can exit the program
const LINE_TO_CHECK = 28;
while (device.run()) {
num_of_executions++;
let d = device.registers[3];
if (device.registers[instruction_pointer] === LINE_TO_CHECK) {
if (!stops[d]) {
last_stop_seen = d;
stops[d] = num_of_executions;
stops_length++;
process.stdout.write(
'Running, found ' +
stops_length.toLocaleString().cyan +
' values of A where the program will halt\r'
);
} else {
console.log('\n\n');
console.log('Register D started looping, so we can exit!\n');
console.log('Last unique possible value that\'d halt the program was:');
console.log(last_stop_seen.toString().green);
break;
}
}
}