-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
93 lines (79 loc) · 1.98 KB
/
index.ts
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
import { chunk } from 'lodash';
import {
INPUT_PATH,
runner,
SAMPLE_PATH,
splitLines,
} from '../../../lib/utils';
const path = `${__dirname}/${INPUT_PATH}`;
type Register = Record<string, number>;
const getCombo = (op: number, registers: Register) => {
if (op <= 3) return op;
switch (op) {
case 4:
return registers.A;
case 5:
return registers.B;
case 6:
return registers.C;
case 7:
throw new Error('Invalid code');
default:
throw new Error('Unexpected op');
}
};
const getOut = (
opcode: number,
operand: number,
registers: Register,
pointer: { idx: number },
) => {
switch (opcode) {
case 0:
registers.A = registers.A >> getCombo(operand, registers);
break;
case 1:
registers.B = registers.B ^ operand;
break;
case 2:
registers.B = getCombo(operand, registers) % 8;
break;
case 3:
if (registers.A !== 0) pointer.idx = operand - 1;
break;
case 4:
registers.B = registers.B ^ registers.C;
break;
case 5:
return getCombo(operand, registers) % 8;
case 6:
registers.B = registers.A >> getCombo(operand, registers);
break;
case 7:
registers.C = registers.A >> getCombo(operand, registers);
break;
}
};
const solution = (input: string) => {
const [registersRaw, programRaw] = input.split('\n\n');
const program = chunk(programRaw.match(/\d/g)?.map(Number), 2);
const registers: Register = {};
registersRaw.split('\n').forEach((row) => {
const [, reg, val] = row.replace(':', '').split(' ');
registers[reg] = +val;
});
const result: number[] = [];
const pointer = { idx: 0 };
while (pointer.idx < program.length) {
const [opcode, operand] = program[pointer.idx];
const out = getOut(opcode, operand, registers, pointer);
if (out !== undefined) result.push(out);
pointer.idx++;
}
return result.join(',');
};
runner({
path,
solution: (input) => solution(input),
});
// 117440