-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21.ts
201 lines (185 loc) · 4.67 KB
/
21.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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;
const HALT_CODE = 1950399;
class IntcodeComputer {
pos: number = 0;
output: number = 0;
outputStr: string = '';
halted: boolean = false;
relBase: number = 0;
program: number[];
constructor(program: number[]) {
this.program = program;
}
isHalted(): boolean {
return this.halted;
}
getP(mode: number, pos: number): number {
switch (mode) {
case IMM_MODE:
return pos;
case POS_MODE:
return this.program[pos];
case REL_MODE:
return this.program[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;
}
run(inputFn: () => number): number {
while (true) {
const op = this.program[this.pos] % 100;
let mode = Math.floor(this.program[this.pos] / 100);
switch (op) {
case ADD: {
let [p1, p2, p3] = this.getPs(mode, this.pos, 3);
const sum = this.program[p1] + this.program[p2];
this.program[p3] = sum;
this.pos += 4;
break;
}
case MULT: {
let [p1, p2, p3] = this.getPs(mode, this.pos, 3);
const product = this.program[p1] * this.program[p2];
this.program[p3] = product;
this.pos += 4;
break;
}
case SAVE: {
let p1 = this.getP(mode, this.pos + 1);
this.program[p1] = inputFn();
this.pos += 2;
break;
}
case OUTPUT: {
let p1 = this.getP(mode, this.pos + 1);
this.output = this.program[p1];
this.outputStr += String.fromCharCode(this.output);
this.pos += 2;
return this.output;
}
case JUMP_IF_TRUE: {
let [p1, p2] = this.getPs(mode, this.pos, 2);
if (this.program[p1] != 0) {
this.pos = this.program[p2];
} else {
this.pos += 3;
}
break;
}
case JUMP_IF_FALSE: {
let [p1, p2] = this.getPs(mode, this.pos, 2);
if (this.program[p1] == 0) {
this.pos = this.program[p2];
} else {
this.pos += 3;
}
break;
}
case LESS_THAN: {
let [p1, p2, p3] = this.getPs(mode, this.pos, 3);
this.program[p3] = this.program[p1] < this.program[p2] ? 1 : 0;
this.pos += 4;
break;
}
case EQUALS: {
let [p1, p2, p3] = this.getPs(mode, this.pos, 3);
this.program[p3] = this.program[p1] == this.program[p2] ? 1 : 0;
this.pos += 4;
break;
}
case REL_OFFSET: {
let p1 = this.getP(mode, this.pos + 1);
this.relBase += this.program[p1];
this.pos += 2;
break;
}
case HALT:
this.halted = true;
console.log(this.output);
return HALT_CODE;
default:
throw Error("Invalid op: " + op + ", number: " + this.program[this.pos]);
}
}
throw Error("Invalid program");
}
}
class Droid {
map: number[][] = [];
springcode: string;
i: number = 0;
constructor(springcode: string) {
this.springcode = springcode;
}
input(): number {
const inputStr = this.springcode.charAt(this.i);
const inputNum = this.springcode.charCodeAt(this.i);
++this.i;
return inputNum
}
run(program: number[]): number {
let computer = new IntcodeComputer(program);
while (!computer.isHalted()) {
computer.run(() => this.input());
}
// Answer logged to console.
return 0;
}
}
export async function solve(): Promise<string> {
const lines = await readlines('./data/21.txt');
const numbers: number[] = lines[0].split(',').map(Number);
const padded = numbers.concat(Array(1000000).fill(0));
let part2 = true;
let springcode;
if (part2) {
// (!f || e || h) && d && !c <- !c && d && (!e && f && !h)
// || !(!d || b || e) <- !b && d && e
// || !a <- !a
springcode = `NOT F J
OR E J
OR H J
AND D J
NOT C T
AND T J
NOT D T
OR B T
OR E T
NOT T T
OR T J
NOT A T
OR T J
RUN
`;
} else {
// !c && d
// || !a
springcode = `NOT C J
AND D J
NOT A T
OR T J
WALK
`;
}
return String(new Droid(springcode).run(padded));
}