-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.ts
252 lines (211 loc) · 5.13 KB
/
day15.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
import { input } from './input';
const box = 'O';
const robot = '@';
const wall = '#';
const empty = '.';
const boxLeft = '[';
const boxRight = ']';
const lines = input.split('\n');
const myGrid: string[][] = [];
const myWideGrid: string[][] = [];
let myMoves = '';
let i = 0;
for (let l of lines) {
if (l.startsWith(wall)) {
myGrid.push(l.split(''));
myWideGrid.push([]);
for (let c of l) {
if (c === robot) {
myWideGrid[i].push(c);
myWideGrid[i].push(empty);
} else if (c === box) {
myWideGrid[i].push(boxLeft);
myWideGrid[i].push(boxRight);
} else {
myWideGrid[i].push(c);
myWideGrid[i].push(c);
}
}
i++;
} else {
myMoves = myMoves.concat(l);
}
}
type Point = [number, number];
const directions = new Map<string, Point>([
['<', [0, -1]],
['v', [1, 0]],
['>', [0, 1]],
['^', [-1, 0]],
]);
function nextStep(pnt: Point, mv: string): Point {
const dir = directions.get(mv)!;
return [pnt[0] + dir[0], pnt[1] + dir[1]];
}
function print(grid: string[][]) {
for (let r of grid) {
console.log(r.join(''));
}
}
function part1(grid: string[][], moves: string) {
function swap(l: Point, r: Point) {
const tmp = grid[l[0]][l[1]];
grid[l[0]][l[1]] = grid[r[0]][r[1]];
grid[r[0]][r[1]] = tmp;
}
function doWork(start: Point, move: string) {
const next = nextStep(start, move);
const field = grid[next[0]][next[1]];
if (field == wall) {
return false;
}
if (field == empty) {
swap(start, next);
return true;
}
if (doWork(next, move)) {
swap(start, next);
return true;
}
return false;
}
let rp: Point = [-1, -1];
// find robot
for (let i = 0; i < grid.length; i++) {
const row = grid[i];
for (let j = 0; j < row.length; j++) {
const c = row[j];
if (c === robot) {
rp = [i, j];
break;
}
}
}
for (let c of moves) {
if (doWork(rp, c)) {
rp = nextStep(rp, c);
}
}
print(grid);
let result = 0;
for (let i = 0; i < grid.length; i++) {
const row = grid[i];
for (let j = 0; j < row.length; j++) {
const c = row[j];
if (c === box) {
result += i * 100 + j;
}
}
}
return result;
}
function part2(grid: string[][], moves: string) {
function swap(l: Point, r: Point) {
const tmp = grid[l[0]][l[1]];
grid[l[0]][l[1]] = grid[r[0]][r[1]];
grid[r[0]][r[1]] = tmp;
}
function canMove(start: Point, move: string): boolean {
const next = nextStep(start, move);
const current = grid[start[0]][start[1]];
if (current == wall) {
return false;
}
if (current == empty) {
return true;
}
// handle boxes special for vert
if (move === '^' || move === 'v') {
if (current === boxLeft) {
if (canMove(next, move) && canMove([next[0], next[1] + 1], move)) {
return true;
}
return false;
}
if (current == boxRight) {
if (canMove(next, move) && canMove([next[0], next[1] - 1], move)) {
return true;
}
return false;
}
}
if (canMove(next, move)) {
return true;
}
return false;
}
function doWork(start: Point, move: string): boolean {
const next = nextStep(start, move);
const current = grid[start[0]][start[1]];
if (current == wall) {
return false;
}
if (current == empty) {
return true;
}
// handle boxes special for vert
if (move === '^' || move === 'v') {
if (current === boxLeft) {
if (doWork(next, move) && doWork([next[0], next[1] + 1], move)) {
swap(start, next);
swap([start[0], start[1] + 1], [next[0], next[1] + 1]);
return true;
}
return false;
}
if (current == boxRight) {
if (doWork(next, move) && doWork([next[0], next[1] - 1], move)) {
swap(start, next);
swap([start[0], start[1] - 1], [next[0], next[1] - 1]);
return true;
}
return false;
}
}
if (doWork(next, move)) {
swap(start, next);
return true;
}
return false;
}
let rp: Point = [-1, -1];
// find robot
for (let i = 0; i < grid.length; i++) {
const row = grid[i];
for (let j = 0; j < row.length; j++) {
const c = row[j];
if (c === robot) {
rp = [i, j];
break;
}
}
}
for (let c of moves) {
// console.log('--------------------------------------------------');
// console.log(`step ${c}`);
// print(grid);
if (canMove(rp, c)) {
if (!doWork(rp, c)) {
throw new Error();
}
rp = nextStep(rp, c);
}
// print(grid);
}
let result = 0;
for (let i = 0; i < grid.length; i++) {
const row = grid[i];
for (let j = 0; j < row.length; j++) {
const c = row[j];
if (c === boxLeft) {
// grid[i][j] = 'X';
// console.log(JSON.stringify({ i, j }));
result += i * 100 + j;
}
}
}
print(grid);
return result;
}
// console.log(part1(structuredClone(myGrid), myMoves));
console.log(part2(structuredClone(myWideGrid), myMoves));