-
Notifications
You must be signed in to change notification settings - Fork 2
/
cst-3.ts
38 lines (31 loc) · 880 Bytes
/
cst-3.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
export function haunted(source: string) {
type Cell = [row: number, col: number, cell: "S" | "E" | "." | "#"]
const map = source
.split("\n")
.map(
(x, i) => [i, x.split("").map((y, j) => [i, j, y] as const)] as const,
) as [row: number, data: Cell[]][]
const start = map
.map(([, row]) => row.find(([, , cell]) => cell))
.find((x): x is Exclude<typeof x, undefined> => x != null)
let exploredCells: Cell[] = [start!]
let size = 0
while (size < 100) {
size++
exploredCells = [
...new Set(
exploredCells
.flatMap(([i, j]) => [
map[i - 1]?.[1][j],
map[i + 1]?.[1][j],
map[i]![1][j - 1],
map[i]![1][j + 1],
])
.filter((x) => x) as any,
),
] as any
if (exploredCells.some((x) => x[2] == "E")) {
return size
}
}
}