-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.ts
288 lines (263 loc) · 7.16 KB
/
15.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import readlines from './util/readlines';
import Map2d from './util/map2d';
import Vec2d from './util/vec2d';
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 BLACK = 0;
const WHITE = 1;
const HALT_CODE = 1950399;
const NORTH = 1;
const SOUTH = 2;
const WEST = 3;
const EAST = 4;
const WALL = 0;
const STEP = 1;
const OXYGEN = 2;
class IntcodeComputer {
pos: number = 0;
output: number = 0;
halted: boolean = false;
relBase: number = 0;
program: number[];
constructor(numbers: number[]) {
this.program = numbers;
}
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;
}
solve(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.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;
return HALT_CODE;
default:
throw Error("Invalid op: " + op + ", number: " + this.program[this.pos]);
}
}
throw Error("Invalid program");
}
}
class Droid {
moves: number = 0;
dir: number = 0;
pos: Vec2d = new Vec2d(0, 0);
checked: Map2d<boolean> = new Map2d();
found: boolean = false;
addChecked(pos: Vec2d) {
this.checked.set(pos, true);
}
input() {
return this.dir;
}
getMove(): Vec2d {
switch (this.dir) {
case NORTH:
return this.pos.add(new Vec2d(0, 1));
case SOUTH:
return this.pos.add(new Vec2d(0, -1));
case EAST:
return this.pos.add(new Vec2d(1, 0));
case WEST:
return this.pos.add(new Vec2d(-1, 0));
}
throw Error('Invalid direction');
}
move() {
this.pos = this.getMove();
}
drawMap(): number[][] {
const keys = Array.from(this.checked.keys());
let xVals = keys.map(k => k.x);
let yVals = keys.map(k => k.y);
let min = new Vec2d(Math.min(...xVals), Math.min(...yVals));
let max = new Vec2d(Math.max(...xVals), Math.max(...yVals));
let size = new Vec2d(max.x - min.x + 1, max.y - min.y + 1);
let img: number[][] = [];
for (let x = 0; x < size.y; ++ x) {
let row: number[] = [];
img[x] = row;
for (let y = 0; y < size.y; ++y) {
row[y] = 0;
}
}
for (let pos of this.checked.keys()) {
img[pos.x - min.x][pos.y - min.y] = 1;
}
img[18 - min.x][-18 - min.y] = 2; // Oxygen
return img;
}
mapArea(program: number[]): number[][] {
let amp = new IntcodeComputer(program);
while (!amp.isHalted() && this.moves < 10000000) {
this.dir = Math.floor(Math.random() * 4) + 1;
while (this.checked.has(this.getMove())) {
this.dir = Math.floor(Math.random() * 4) + 1;
}
let status: number = amp.solve(() => this.input());
switch (status) {
case WALL:
this.addChecked(this.getMove());
break;
case STEP:
++this.moves;
this.move();
break;
case OXYGEN:
++this.moves;
this.move();
this.found = true;
break;
}
}
return this.drawMap();
}
}
function visit(pos: [number, number], visited: boolean[][]) {
let y = visited[pos[0]];
if (!y) {
y = [];
visited[pos[0]] = y;
}
y[pos[1]] = true;
}
function isVisited(pos: [number, number], visited: boolean[][]) {
return visited[pos[0]] && visited[pos[0]][pos[1]];
}
function isOpen(pos: [number, number], map: number[][]) {
return map[pos[0]] && map[pos[0]][pos[1]] === 0;
}
function isValid(map: number[][], pos: [number, number], visited: boolean[][]) {
return !isVisited(pos, visited) && isOpen(pos, map);
}
function bfs(map: number[][], start: [number, number]): number {
let visited: boolean[][] = [];
visit(start, visited);
let q: [number, number, number][] = [];
q.push([start[0], start[1], 0]);
let max = -1;
while (q.length != 0) {
let [x, y, dist] = q.shift()!;
if (dist > max) {
max = dist;
}
let adj: [number, number][] = [[x - 1, y], [x + 1, y], [x, y + 1], [x, y - 1]];
for (let pos of adj) {
if (isValid(map, pos, visited)) {
visit(pos, visited);
q.push([pos[0], pos[1], dist + 1]);
}
}
}
return max;
}
export async function solve(): Promise<number> {
const lines = await readlines('./data/15.txt');
const numbers: number[] = lines[0].split(',').map(Number);
const padded = numbers.concat(Array(1000000).fill(0));
const map: number[][] = new Droid().mapArea(padded);
let start: [number, number] = [-1, -1];
for (let x = 0; x < map.length; ++x) {
for (let y = 0; y < map[x].length; ++y) {
if (map[x][y] === 2) {
start = [x, y];
break;
}
}
}
return bfs(map, start);
}