-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_17.mjs
79 lines (64 loc) · 1.96 KB
/
day_17.mjs
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
import fs from 'node:fs';
class Program {
constructor([registerData, programData]) {
this.register = {};
[this.register.A, this.register.B, this.register.C] = registerData.match(/\d+/g).map(Number);
this.instructions = programData.match(/\d+/g).map(Number);
this.pointer = 0;
this.output = [];
}
get formatedOutput() { return this.output.join(','); }
getComboOperand(operand) {
if (operand >= 0 && operand <= 3) return operand;
if (operand === 4) return this.register.A;
if (operand === 5) return this.register.B;
if (operand === 6) return this.register.C;
}
adv(operand) {
this.register.A = Math.trunc(this.register.A / (2 ** this.getComboOperand(operand)));
}
bxl(operand) {
this.register.B ^= operand;
}
bst(operand) {
this.register.B = this.getComboOperand(operand) % 8;
}
jnz(operand) {
if (this.register.A === 0) return false;
this.pointer = operand;
return true;
}
bxc() {
this.register.B ^= this.register.C;
}
out(operand) {
this.output.push(this.getComboOperand(operand) % 8);
}
bdv(operand) {
this.register.B = Math.trunc(this.register.A / (2 ** this.getComboOperand(operand)));
}
cdv(operand) {
this.register.C = Math.trunc(this.register.A / (2 ** this.getComboOperand(operand)));
}
run() {
const opCodeLookup = [
this.adv.bind(this),
this.bxl.bind(this),
this.bst.bind(this),
this.jnz.bind(this),
this.bxc.bind(this),
this.out.bind(this),
this.bdv.bind(this),
this.cdv.bind(this),
];
while (this.pointer < this.instructions.length) {
const opcode = this.instructions[this.pointer];
const operand = this.instructions[this.pointer + 1];
const jumped = opCodeLookup[opcode](operand);
if (!jumped) this.pointer += 2;
}
}
}
const program = new Program(fs.readFileSync('input.txt', 'utf-8').trim().split('\n\n'));
program.run();
console.log(`Part 1: ${program.formatedOutput}`);