-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.ts
150 lines (140 loc) · 3.82 KB
/
9.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import readlines from './util/readlines';
const ADD = 1;
const MULT = 2;
const SAVE = 3;
const OUTPUT = 4;
const JUMP_IF_TRUE = 5;
const JUMP_IF_FALSE = 6;
const LESS_THAN = 7;
const EQUALS = 8;
const REL_OFFSET = 9;
const HALT = 99;
const POS_MODE = 0;
const IMM_MODE = 1;
const REL_MODE = 2;
class Amplifier {
pos: number = 0;
output: number = 0;
numbers: number[];
halted: boolean;
relBase: number = 0;
constructor(numbers: number[]) {
this.pos = 0;
this.output = 0;
this.halted = false;
this.numbers = numbers;
this.relBase = 0;
}
isHalted(): boolean {
return this.halted;
}
getP(mode: number, pos: number): number {
switch (mode) {
case IMM_MODE:
return pos;
case POS_MODE:
return this.numbers[pos];
case REL_MODE:
return this.numbers[pos] + this.relBase;
}
throw Error('Invalid op');
}
getPs(mode: number, pos: number, count: number): number[] {
let out: number[] = [];
for (let i = 1; i <= count; ++i) {
out.push(this.getP(mode % 10, pos + i));
mode = Math.floor(mode / 10);
}
return out;
}
solve(inputs: number[]): number {
while (true) {
const op = this.numbers[this.pos] % 100;
let mode = Math.floor(this.numbers[this.pos] / 100);
switch (op) {
case ADD: {
let [p1, p2, p3] = this.getPs(mode, this.pos, 3);
const sum = this.numbers[p1] + this.numbers[p2];
this.numbers[p3] = sum;
this.pos += 4;
break;
}
case MULT: {
let [p1, p2, p3] = this.getPs(mode, this.pos, 3);
const product = this.numbers[p1] * this.numbers[p2];
this.numbers[p3] = product;
this.pos += 4;
break;
}
case SAVE: {
let p1 = this.getP(mode, this.pos + 1);
this.numbers[p1] = inputs[0];
this.pos += 2;
break;
}
case OUTPUT: {
let p1 = this.getP(mode, this.pos + 1);
this.output = this.numbers[p1];
this.pos += 2;
return this.output;
}
case JUMP_IF_TRUE: {
let [p1, p2] = this.getPs(mode, this.pos, 2);
if (this.numbers[p1] != 0) {
this.pos = this.numbers[p2];
} else {
this.pos += 3;
}
break;
}
case JUMP_IF_FALSE: {
let [p1, p2] = this.getPs(mode, this.pos, 2);
if (this.numbers[p1] == 0) {
this.pos = this.numbers[p2];
} else {
this.pos += 3;
}
break;
}
case LESS_THAN: {
let [p1, p2, p3] = this.getPs(mode, this.pos, 3);
this.numbers[p3] = this.numbers[p1] < this.numbers[p2] ? 1 : 0;
this.pos += 4;
break;
}
case EQUALS: {
let [p1, p2, p3] = this.getPs(mode, this.pos, 3);
this.numbers[p3] = this.numbers[p1] == this.numbers[p2] ? 1 : 0;
this.pos += 4;
break;
}
case REL_OFFSET: {
let p1 = this.getP(mode, this.pos + 1);
this.relBase += this.numbers[p1];
this.pos += 2;
break;
}
case HALT:
this.halted = true;
return this.output;
default:
throw Error("Invalid op: " + op + ", number: " + this.numbers[this.pos]);
}
}
throw Error("Invalid program");
}
}
function runProgram(program: number[], part2: boolean): number {
let amp = new Amplifier([...program]);
let out = 0;
while (!amp.isHalted()) {
out = amp.solve([part2 ? 2 : 1]);
}
return out;
}
export async function solve(): Promise<number> {
const lines = await readlines('./data/9.txt');
const numbers: number[] = lines[0].split(',').map(Number);
const padded = numbers.concat(Array(1000000).fill(0));
return runProgram(padded, true);
}