-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.ts
145 lines (127 loc) · 3.06 KB
/
day16.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
function log(u: unknown) {
console.log(JSON.stringify(u, undefined, 2));
}
const test_input = `.|...\\....
|.-.\\.....
.....|-...
........|.
..........
.........\\
..../.\\\\..
.-.-/..|..
.|....-|.\\
..//.|....`.split('\n');
const puzzle_input = `<get from aoc website>`.split('\n');
type coords = [row: number, col: number];
function fillGrid(
grid: readonly string[],
start: coords,
direction: coords,
): Set<string> {
const cache: Map<string, Set<string>> = new Map();
const loop: Set<string> = new Set();
return fillGridWorker(grid, start, direction);
function fillGridWorker(
grid: readonly string[],
start: coords,
direction: coords,
): Set<string> {
const visited: Set<string> = new Set();
const key = JSON.stringify([start, direction]);
if (cache.has(key)) {
return cache.get(key) ?? new Set<string>();
}
if (loop.has(key)) {
return new Set<string>();
}
loop.add(key);
let d = direction;
let x = start[0] + direction[0];
let y = start[1] + direction[1];
let cnt = 0;
while (
x >= 0 &&
y >= 0 &&
x < grid.length &&
y < grid[0].length &&
cnt < 100_000
) {
visited.add(`${x},${y}`);
const c = grid[x][y];
if (c === '.') {
x += d[0];
y += d[1];
}
if (c === '/') {
d = d[0] === 0 ? [-1 * d[1], 0] : [0, -1 * d[0]];
x += d[0];
y += d[1];
}
if (c === '\\') {
d = d[0] === 0 ? [d[1], 0] : [0, d[0]];
x += d[0];
y += d[1];
}
if (c === '-') {
if (d[0] === 0) {
y += d[1];
} else {
const top = fillGridWorker(grid, [x, y], [0, 1]);
top.forEach((v) => visited.add(v));
const bottom = fillGridWorker(grid, [x, y], [0, -1]);
bottom.forEach((v) => visited.add(v));
break;
}
}
if (c === '|') {
if (d[1] === 0) {
x += d[0];
} else {
const left = fillGridWorker(grid, [x, y], [1, 0]);
left.forEach((v) => visited.add(v));
const right = fillGridWorker(grid, [x, y], [-1, 0]);
right.forEach((v) => visited.add(v));
break;
}
}
}
cache.set(key, visited);
return visited;
}
}
function part1(input: string[]) {
const cnt = fillGrid(input, [0, -1], [0, 1]);
log(cnt.size);
}
function part2(input: string[]) {
const sources: Array<[coords, coords]> = [];
// generate input for all sides
for (let i = 0; i < input.length; i++) {
sources.push([
[i, -1],
[0, 1],
]);
sources.push([
[i, input.length],
[0, -1],
]);
}
for (let i = 0; i < input[0].length; i++) {
sources.push([
[-1, i],
[1, 0],
]);
sources.push([
[input.length, i],
[-1, 0],
]);
}
// map to sums
const sums = sources.map((s) => fillGrid(input, s[0], s[1]));
log({ sums: sums.map((s) => s.size) });
// find max
const max = sums.reduce((a, c) => Math.max(a, c.size), 0);
log(max);
}
// part1(puzzle_input);
part2(puzzle_input);