-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcodes2.js
99 lines (76 loc) · 2.65 KB
/
opcodes2.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
const addr = (a, b, registers) => registers[a] + registers[b];
const addi = (a, b, registers) => registers[a] + b;
const mulr = (a, b, registers) => registers[a] * registers[b];
const muli = (a, b, registers) => registers[a] * b;
const banr = (a, b, registers) => registers[a] & registers[b];
const bani = (a, b, registers) => registers[a] & b;
const borr = (a, b, registers) => registers[a] | registers[b];
const bori = (a, b, registers) => registers[a] | b;
const setr = (a, b, registers) => registers[a];
const seti = (a) => a;
const gtir = (a, b, registers) => a > registers[b] ? 1 : 0;
const gtri = (a, b, registers) => registers[a] > b ? 1 : 0;
const gtrr = (a, b, registers) => registers[a] > registers[b] ? 1 : 0;
const eqir = (a, b, registers) => a === registers[b] ? 1 : 0;
const eqri = (a, b, registers) => registers[a] === b ? 1 : 0;
const eqrr = (a, b, registers) => registers[a] === registers[b] ? 1 : 0;
const opcodes = (input) => {
const lines = input.split('\n').map((line) => line.trim());
let samples = [];
let programStart = 0;
for (let i = 0; i < lines.length; i++) {
if (lines[i].startsWith('Before:')) {
const sample = {
before: lines[i].match(/\[(.*)\]/)[1].split(',').map(Number),
instruction: lines[i + 1].split(' ').map(Number),
after: lines[i + 2].match(/\[(.*)\]/)[1].split(',').map(Number),
};
samples.push(sample);
}
if (!lines[i].length && !lines[i + 2].length) {
programStart = i + 3;
}
}
const sortedOps = Array.from({ length: 16 });
const ops = [
addr, addi,
mulr, muli,
banr, bani,
borr, bori,
setr, seti,
gtir, gtri, gtrr,
eqri, eqir, eqrr,
];
let i = 0;
while (ops.length) {
const op = ops[i];
const matchingSamples = samples
.filter(({ before, instruction, after }) => {
const [, a, b, c] = instruction;
const registers = [...before];
registers[c] = op(a, b, registers);
return registers.join() === after.join();
});
const opCodes = new Set(matchingSamples.map(({ instruction }) => instruction[0]));
if (opCodes.size === 1) {
const opCode = [...opCodes][0];
sortedOps[opCode] = op;
ops.splice(i, 1);
samples = samples.filter(({ instruction }) => instruction[0] !== opCode);
}
i++;
if (i >= ops.length) {
i = 0;
}
}
const registers = [0, 0, 0, 0];
lines
.slice(programStart, lines.length)
.map((line) => line.split(' ').map(Number))
.forEach((instruction) => {
const [opCode, a, b, c] = instruction;
registers[c] = sortedOps[opCode](a, b, registers);
});
return registers;
};
module.exports = opcodes;