-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathreg-d-on-ip-6.js
58 lines (48 loc) · 1.7 KB
/
reg-d-on-ip-6.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
// const { program } = require('./sample-input');
const { program } = require('./input');
const DeviceInstructions = require('./device-instructions');
const readline = require('readline');
let instruction_pointer = program.shift();
instruction_pointer = instruction_pointer[1];
const regs = [11474091, 0, 18, 14290240, 65536, 0];
// const regs = [0, 0, 0, 0, 0, 0];
let device = new DeviceInstructions(program, regs);
device.setInputPointer(instruction_pointer);
const outputArrayWithPadding = (arr, padding = 16) => {
let log = '';
for (let i = 0; i < arr.length; i++) {
log += String(arr[i]).padEnd(padding);
}
console.log(log);
};
const watchRegisters = (function() {
let registers_copy = device.registers.slice(0);
return (...indices) => {
for (let i = 0; i < indices.length; i++) {
let index = indices[i];
if (device.registers[index] !== registers_copy[index]) {
outputArrayWithPadding(device.registers);
registers_copy[index] = device.registers[index];
}
}
};
})();
let smallest_d = Number.MAX_SAFE_INTEGER;
let num_of_executions = 0;
while (device.run()) {
num_of_executions++;
let [a, b, c, d, e, f] = device.registers;
if (device.registers[instruction_pointer] === 6) {
// console.log(d);
if (d < smallest_d) {
// process.stdout.write(d + ' \r');
console.log(d);
smallest_d = d;
}
}
}
console.log('\nI FINISHED!');
console.log('Executed ' + num_of_executions + ' instructions');
console.log('Register 0 is: ' + device.registers[0]);
console.log('All registers listed below: ');
console.log(device.registers);