-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday20.ts
109 lines (86 loc) · 3.02 KB
/
day20.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
import type { Day } from './Day.ts';
import { getPositionOfUniqElement } from './utils.ts';
interface Point { x: number; y: number }
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({ atLeast = 100 } = {}) {
const findStart = getPositionOfUniqElement(this.input, 'S');
const distances = bfs(this.input, { x: findStart[1], y: findStart[0] });
let cheats = 0;
const walkable = Object.keys(distances);
for (let i = 0; i < walkable.length; i++) {
for (let j = 0; j < walkable.length; j++) {
if (i === j) {
continue;
}
const start = walkable[i].split(',').map(num => Number.parseInt(num));
const end = walkable[j].split(',').map(num => Number.parseInt(num));
const dist = Math.abs(start[0] - end[0]) + Math.abs(start[1] - end[1]);
if (dist <= 2 && distances[walkable[i]] - distances[walkable[j]] - dist >= atLeast)
cheats++;
}
}
return cheats;
}
partTwo({ atLeast = 100 } = {}) {
const findStart = getPositionOfUniqElement(this.input, 'S');
const distances = bfs(this.input, { x: findStart[1], y: findStart[0] });
let cheats = 0;
const walkable = Object.keys(distances);
for (let i = 0; i < walkable.length; i++) {
for (let j = 0; j < walkable.length; j++) {
if (i === j) {
continue;
}
const start = walkable[i].split(',').map(num => Number.parseInt(num));
const end = walkable[j].split(',').map(num => Number.parseInt(num));
const dist = Math.abs(start[0] - end[0]) + Math.abs(start[1] - end[1]);
if (dist <= 20 && distances[walkable[i]] - distances[walkable[j]] - dist >= atLeast)
cheats++;
}
}
return cheats;
}
}
function bfs(grid: string[][], start: Point) {
const height = grid.length;
const width = grid[0].length;
const queue: { x: number; y: number; steps: number }[] = [];
const distances: { [key: string]: number } = {};
queue.push({ ...start, steps: 0 });
distances[`${start.x},${start.y}`] = 0;
while (queue.length !== 0) {
const current = queue.shift();
if (current === undefined)
break;
[
[1, 0],
[0, 1],
[-1, 0],
[0, -1],
].forEach(([y, x]) => {
const position = { x: current.x + x, y: current.y + y };
if (position.x < 0 || position.x >= width || position.y < 0 || position.y >= height || grid[position.y][position.x] === '#')
return;
const newDistance = current.steps + 1;
const key = `${position.x},${position.y}`;
if (distances[key] === undefined || distances[key] > newDistance) {
queue.push({ ...position, steps: current.steps + 1 });
distances[`${position.x},${position.y}`] = newDistance;
}
});
}
return distances;
}