-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21.ts
232 lines (200 loc) · 6.2 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
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
import { COLOR, SUM, isPresent, mod } from "../utils/helpers.js";
import Task, { TaskPartSolution } from "../utils/task.js";
import readline from "readline";
type Map = boolean[][]; // true - wall, false - empty
type Pos = [number, number]; // [x, y]
const parseInput = (input: string): [Map, Pos] => {
let pos: Pos;
const map = input.split("\n").map((r, y) =>
r.split("").map((c, x) => {
if (c === "S") {
pos = [x, y];
}
return c === "#";
})
);
return [map, pos!];
};
const N = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
];
const neighbors = (p: Pos): Pos[] => N.map((d) => [p[0] + d[0], p[1] + d[1]]);
const step = (positions: Pos[], map: Map): Pos[] => {
const width = map[0].length;
const height = map.length;
const posMap: Map = new Array(height).fill(1).map(() => new Array(width).fill(1).map(() => false));
positions
.flatMap((p) => neighbors(p).filter(([x, y]) => x >= 0 && x < width && y >= 0 && y < height))
.forEach((p) => (posMap[p[1]][p[0]] = true));
return posMap
.flatMap((row, y) => row.map((v, x): Pos | null => (v === true && map[y][x] === false ? [x, y] : null)))
.filter(isPresent);
};
const part1: TaskPartSolution = (input) => {
const [map, start] = parseInput(input);
let positions = [start];
for (let i = 0; i < 64; i++) {
positions = step(positions, map);
console.log(i, positions.length);
}
return positions.length;
};
type PositionsMap = Record<string, Record<string, boolean>>;
type Chunks = Record<string, { x: number; y: number; positions: PositionsMap }>;
const CHUNK_CACHE: Record<string, Pos[]> = {};
const printMap = (map: Map, positions: Pos[]) => {
const posMap: { [y: number]: { [x: number]: boolean } } = {};
positions
.flatMap((p) => neighbors(p))
.forEach(([px, py]) => {
if (!posMap[py]) posMap[py] = {};
posMap[py][px] = true;
});
for (let y = 0; y < map.length; y++) {
for (let x = 0; x < map[y].length; x++) {
const c = map[y][x];
process.stdout.write(c ? `${COLOR.Reset}#` : `${posMap[y]?.[x] ? COLOR.BgGreen : COLOR.Reset}.`);
}
process.stdout.write(`${COLOR.Reset}\n`);
}
};
function prompt() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) =>
rl.question("Continue?", (ans) => {
rl.close();
if (ans === "n") process.exit(0);
resolve(ans);
})
);
}
const stepChunk = async (positions: Pos[], map: Map, debug?: boolean): Promise<Pos[]> => {
if (debug) {
// console.log(getBounds(positions));
// console.log(positions);
printMap(map, positions);
await prompt();
}
const chunkKey = positions.map((p) => `${p[0]}_${p[1]}`).join("|");
if (CHUNK_CACHE[chunkKey]) return CHUNK_CACHE[chunkKey];
const width = map[0].length;
const height = map.length;
const posMap: { [y: number]: { [x: number]: boolean } } = {};
positions
.flatMap((p) => neighbors(p))
.forEach(([px, py]) => {
if (!posMap[py]) posMap[py] = {};
posMap[py][px] = true;
});
const res = Object.entries(posMap)
.flatMap(([ys, row]) =>
Object.entries(row).map(([xs, v]): Pos | null => {
const x = Number(xs);
const y = Number(ys);
if (v === true && map[mod(y, height)][mod(x, width)] === false) {
return [x, y];
}
return null;
})
)
.filter(isPresent);
CHUNK_CACHE[chunkKey] = res;
return res;
};
const getBounds = (positions: Pos[]) => {
return [
[Math.min(...positions.map((p) => p[0])), Math.min(...positions.map((p) => p[1]))],
[Math.max(...positions.map((p) => p[0])), Math.max(...positions.map((p) => p[1]))],
] as const;
};
const posChunk = (pos: Pos, width: number, height: number): Pos => {
return [pos[0] - mod(pos[0], width), pos[1] - mod(pos[1], height)];
};
const step1 = async (chunks: Chunks, map: Map): Promise<Chunks> => {
const width = map[0].length;
const height = map.length;
Object.entries(chunks).forEach((chunk) => {});
// console.log(bounds);
// console.log("step1");
// align x and y with chunks
for (let x = bounds[0][0] - mod(bounds[0][0], width); x <= bounds[1][0]; x += width) {
for (let y = bounds[0][1] - mod(bounds[0][1], height); y <= bounds[1][1]; y += height) {
const chunkMin = [x - 1, y - 1];
const chunkMax = [x + width, y + width];
// console.log(`chunk[${x},${y}]`, chunkMin, chunkMax);
const chunkPos = positions
.filter(([px, py]) => px >= chunkMin[0] && px <= chunkMax[0] && py >= chunkMin[1] && py <= chunkMax[1])
.map(([px, py]): Pos => [px - x, py - y]);
if (chunkPos.length === 0) {
continue;
}
(await stepChunk(chunkPos, map))
.map(([px, py]) => [px + x, py + y])
.forEach(([px, py]) => {
if (!posMap[py]) posMap[py] = {};
posMap[py][px] = true;
});
}
}
return Object.entries(posMap)
.flatMap(([ys, row]) =>
Object.entries(row).map(([xs, v]): Pos | null => {
const x = Number(xs);
const y = Number(ys);
return v === true && map[mod(y, height)][mod(x, width)] === false ? [x, y] : null;
})
)
.filter(isPresent);
};
const getSum = (chunks: Chunks): number => {
return SUM(Object.values(chunks).map((c) => SUM(Object.entries(c.positions).map((r) => Object.values(r[1]).length))));
};
const part2: TaskPartSolution = async (input) => {
const [map, start] = parseInput(input);
let chunks: Chunks = { "0_0": { x: 0, y: 0, positions: { [start[1]]: { [start[0]]: true } } } };
for (let i = 0; i < 26501365; i++) {
// for (let i = 0; i < 10; i++) {
chunks = await step1(chunks, map);
if ([6, 10, 50, 100, 500, 1000, 5000].includes(i + 1) || i % 100 === 0) {
console.log(i + 1, getSum(chunks));
}
}
return getSum(chunks);
};
const task = new Task(2023, 21, part1, part2, {
part1: {
input: `...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........`,
result: "16",
},
part2: {
input: `...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........`,
result: "???",
},
});
export default task;