-
Notifications
You must be signed in to change notification settings - Fork 0
/
a.ts
134 lines (108 loc) · 3.18 KB
/
a.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
import { assertEquals } from "https://deno.land/std@0.167.0/testing/asserts.ts";
type Point = [number, number];
const stringify = (a: Point) => {
return `[${a[0]},${a[1]}]`;
};
interface Args {
grid: string[];
source: Point;
isEnd: (node: Point) => boolean;
childFilter: (child: Point, parent: Point) => boolean;
}
function findShortestDistance({ grid, source, isEnd, childFilter }: Args) {
const distances = new Map<string, number>();
distances.set(stringify(source), 0);
const queue: Point[] = [];
queue.push(source);
const previous = new Map<string, Point>();
const visited = new Set<string>();
while (queue.length > 0) {
const node = queue.shift()!;
const [x, y] = node;
const nodeStr = stringify(node);
if (visited.has(nodeStr)) {
continue;
}
if (isEnd(node)) {
return {
shortestDistance: distances.get(nodeStr),
distances,
previous,
};
}
visited.add(nodeStr);
const children = [
[x + 1, y] as Point,
[x - 1, y] as Point,
[x, y + 1] as Point,
[x, y - 1] as Point,
]
.filter(([x, y]) =>
x >= 0 && x < grid[0].length && y >= 0 && y < grid.length
)
.filter((child) => childFilter(child, node));
for (const neighbor of children) {
const neighborStr = stringify(neighbor);
const distance = distances.get(nodeStr)! + 1;
if (
!distances.has(neighborStr) || distance < distances.get(neighborStr)!
) {
distances.set(neighborStr, distance);
previous.set(neighborStr, node);
queue.push(neighbor);
}
}
}
throw new Error(
`Can't find shortest path from '${stringify(source)}' to target point`,
);
}
function solution(input: string) {
const lines = input.split("\n");
const findStartAndEnd = () => {
const result = {} as { start: Point; end: Point };
for (let i = 0; i < lines.length; i++) {
const indexS = lines[i].indexOf("S");
const indexE = lines[i].indexOf("E");
if (indexS !== -1) {
result.start = [indexS, i];
}
if (indexE !== -1) {
result.end = [indexE, i];
}
}
return result as { start: Point; end: Point };
};
const { start } = findStartAndEnd();
const { shortestDistance } = findShortestDistance({
grid: lines,
source: start,
isEnd: (node) => lines[node[1]][node[0]] === "E",
childFilter: (child, parent) => {
const [childHeight, parentHeight] = [child, parent].map(([x, y]) => {
const value = lines[y][x];
if (value === "S") {
return "a".charCodeAt(0);
}
if (value === "E") {
return "z".charCodeAt(0);
}
return value.charCodeAt(0);
});
return childHeight <= parentHeight + 1;
},
});
return shortestDistance;
}
Deno.test("example", () => {
const input = Deno.readTextFileSync("./12/example.txt");
const actual = solution(input);
const expected = 31;
assertEquals(actual, expected);
});
Deno.test("puzzle input", { ignore: false }, () => {
const input = Deno.readTextFileSync("./12/input.txt");
const actual = solution(input);
const expected = 383;
assertEquals(actual, expected);
});