-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.ts
135 lines (104 loc) · 3.42 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
import type { Day } from './Day.ts';
import { enumGrid, getPositionOfUniqElement, isInGrid, MinHeap, type Pos } from './utils.ts';
interface AdjacencyList { [key: string]: { [key: string]: number } }
// EAST, SOUTH, WEST, NORTH
const DIRECTIONS: Pos[] = [[0, 1], [1, 0], [0, -1], [-1, 0]];
export class DayImpl implements Day {
private readonly input: string[][];
constructor(input: string) {
this.input = this.parseInput(input);
}
parseInput(input: string) {
return input
.trim()
.split('\n')
.map((line: string) => {
return line
.split('');
});
}
partOne() {
const { start, end, forward } = parseGrid(this.input);
const distances = dijkstra(forward, start, false);
return distances[getPosKey(end)];
}
partTwo() {
const { start, end, forward, reverse } = parseGrid(this.input);
const fromStart = dijkstra(forward, start, false);
const toEnd = dijkstra(reverse, end, true);
const endKey = getPosKey(end);
const target = fromStart[endKey];
const spaces = new Set<string>();
Object.keys(fromStart).forEach((position) => {
if (position !== endKey && fromStart[position] + toEnd[position] === target) {
const [x, y] = position.split(',');
spaces.add(`${x},${y}`);
}
});
return spaces.size;
}
}
function parseGrid(grid: string[][]) {
const start: Pos = getPositionOfUniqElement(grid, 'S');
const end: Pos = getPositionOfUniqElement(grid, 'E');
const forward: AdjacencyList = {};
const reverse: AdjacencyList = {};
for (const { x, y, cell } of enumGrid(grid)) {
if (cell === '#') {
continue;
}
DIRECTIONS.forEach(([y2, x2], i) => {
const [pY, pX] = [y + y2, x + x2];
const key = `${y},${x},${i}`;
const moveKey = `${pY},${pX},${i}`;
if (isInGrid(grid, pY, pX) && grid[pY][pX] !== '#') {
forward[key] ??= {};
reverse[moveKey] ??= {};
forward[key][moveKey] = 1;
reverse[moveKey][key] = 1;
}
for (const rotateKey of [`${y},${x},${(i + 3) % 4}`, `${y},${x},${(i + 1) % 4}`]) {
forward[key] ??= {};
reverse[rotateKey] ??= {};
forward[key][rotateKey] = 1000;
reverse[rotateKey][key] = 1000;
}
});
}
DIRECTIONS.forEach((_, i) => {
const key = getPosKey(end);
const rotateKey = `${key},${i}`;
forward[rotateKey] ??= {};
reverse[key] ??= {};
forward[rotateKey][key] = 0;
reverse[key][rotateKey] = 0;
});
return { start, end, forward, reverse };
}
function getPosKey(pos: Pos) {
return `${pos[0]},${pos[1]}`;
}
function dijkstra(graph: AdjacencyList, start: Pos, directionless: boolean): { [key: string]: number } {
const queue = new MinHeap();
const distances: { [key: string]: number } = {};
const startingKey = directionless ? getPosKey(start) : `${getPosKey(start)},0`;
queue.insert({ score: 0, node: startingKey });
distances[startingKey] = 0;
while (queue.size()) {
const current = queue.extractMin();
if (distances[current.node] < current.score) {
continue;
}
if (!graph[current.node]) {
continue;
}
Object.entries(graph[current.node]).forEach(([next, weight]) => {
const newScore = current.score + weight;
if (distances[next] === undefined || distances[next] > newScore) {
distances[next] = newScore;
queue.insert({ score: newScore, node: next });
}
});
}
return distances;
}