-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
53 lines (40 loc) · 1.09 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
function add(a: number, b: number): number {
return a + b;
}
function multiply(a: number, b: number): number {
return a * b;
}
function part1(opcodes: number[], noun = 12, verb = 2): number {
const codes = [...opcodes];
codes[1] = noun;
codes[2] = verb;
let position = 0;
while (codes[position] !== 99) {
const opCode = codes[position];
const inputAIndex = codes[position + 1];
const inputA = codes[inputAIndex];
const inputBIndex = codes[position + 2];
const inputB = codes[inputBIndex];
const outputIndex = codes[position + 3];
if (opCode === 1) {
codes[outputIndex] = add(inputA, inputB);
}
if (opCode === 2) {
codes[outputIndex] = multiply(inputA, inputB);
}
position += 4;
}
return codes[0];
}
function part2(opcodes: number[], target: number): number {
for (let noun = 0; noun < 100; noun += 1) {
for (let verb = 0; verb < 100; verb += 1) {
const result = part1(opcodes, noun, verb);
if (result === target) {
return 100 * noun + verb;
}
}
}
return -1;
}
export { part1, part2 };