-
Notifications
You must be signed in to change notification settings - Fork 0
/
d04.ts
59 lines (53 loc) · 1.45 KB
/
d04.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
import * as _ from "jsr:@es-toolkit/es-toolkit";
function parse(input: string): string[][] {
const arr = input.split("\n").map((line) => ["", ...line.split(""), ""]);
return [Array(arr.length).fill(""), ...arr, Array(arr.length).fill("")];
}
function _search(
soup: string[][],
word: string,
[x, y]: number[],
[dx, dy]: number[],
vis = [[x, y]]
): number[][] {
if (word.length === 1) {
return vis;
}
if (soup[x + dx][y + dy] !== word[1]) {
return [];
}
return _search(
soup,
word.slice(1),
[x + dx, y + dy],
[dx, dy],
[...vis, [x + dx, y + dy]]
);
}
function search(
soup: string[][],
word: string,
pattern: number[][]
): number[][][] {
return soup.flatMap((row, i) =>
row.flatMap((cell, j) =>
cell === word[0]
? pattern
.map((dir) => _search(soup, word, [i, j], dir))
.filter((res) => res.length > 0)
: []
)
);
}
export function p1(input: string): number {
const pattern = [-1, 0, 1].flatMap((dx) =>
[-1, 0, 1].map((dy) => [dx, dy]).filter(([dx, dy]) => dx !== 0 || dy !== 0)
);
return search(parse(input), "XMAS", pattern).length;
}
export function p2(input: string): number {
const pattern = [-1, 1].flatMap((dx) => [-1, 1].map((dy) => [dx, dy]));
const words = search(parse(input), "MAS", pattern);
const centers = _.countBy(words, ([_, center]) => center.join(" "));
return Object.values(centers).filter((count) => count === 2).length;
}