-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.ts
127 lines (103 loc) · 2.68 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
import { Heap } from 'heap-js';
import { input } from './input';
type Point = [x: number, y: number];
const myGrid = input.split('\n').map((r) => r.split(''));
const dirs: Map<string, Point> = new Map([
['E', [0, 1]],
['S', [1, 0]],
['N', [-1, 0]],
['W', [0, -1]],
]);
function print(grid: string[][]) {
for (let r of grid) {
console.log(r.join(''));
}
}
function part1(grid: string[][]) {
function getField(p: Point) {
return grid[p[0]][p[1]];
}
const start: Point = [grid.length - 2, 1];
const initialDir = 'E';
if (getField(start) !== 'S') {
throw new Error();
}
const visited = new Map<string, number>();
const result = [];
let minCost = Number.MAX_VALUE;
const q = new Heap<[Point, string, number, Point[]]>((l, r) => l[2] - r[2]);
q.push([start, initialDir, 0, []]);
while (q.length > 0) {
const [pos, dir, cost, [...path]] = q.pop()!;
// don't bother if the total is going to be higher
if (cost > minCost) {
break;
}
const field = getField(pos);
// reached end
if (field === 'E') {
if (cost < minCost) {
minCost = cost;
}
path.push(pos);
result.push({ path, cost });
continue;
}
// reached wall
if (field === '#') {
continue;
}
// visited before ignore if last visit was cheaper
const key = `${pos}@${dir}`;
if (visited.has(key)) {
if (visited.get(key)! < cost) {
continue;
}
}
visited.set(key, cost);
path.push(pos);
// next steps
for (let [c, dxdy] of dirs.entries()) {
if (
(dir === 'E' && c === 'W') ||
(dir === 'W' && c === 'E') ||
(dir === 'N' && c === 'S') ||
(dir === 'S' && c === 'N')
) {
// don't backtrack
continue;
}
let nextPos: Point = [0, 0];
let newCost = 0;
if (c === dir) {
nextPos = [pos[0] + dxdy[0], pos[1] + dxdy[1]];
newCost = cost + 1;
}
if (dir === 'E' || dir === 'W') {
if (c === 'N' || c === 'S') {
nextPos = [pos[0] + dxdy[0], pos[1] + dxdy[1]];
newCost = cost + 1001;
}
}
if (dir === 'N' || dir === 'S') {
if (c === 'E' || c === 'W') {
nextPos = [pos[0] + dxdy[0], pos[1] + dxdy[1]];
newCost = cost + 1001;
}
}
if (getField(nextPos) !== '#') {
q.push([nextPos, c, newCost, path]);
}
}
}
const spots = new Set<string>();
for (let route of result) {
for (let s of route.path) {
const key = JSON.stringify(s);
spots.add(key);
}
}
// print(grid);
return { part1: minCost, part2: spots.size };
}
console.log(part1(structuredClone(myGrid)));