-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
70 lines (59 loc) · 1.54 KB
/
index.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
export function part1(input: string) {
const g = grid(input)
const rows = g.length
const cols = g[0].length
let count = 0
const word = 'XMAS'
const directions = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
[1, 1],
[-1, -1],
[1, -1],
[-1, 1],
]
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
for (const [dr, dc] of directions) {
let i = 0
for (; i < word.length; i++) {
const r = row + dr * i
const c = col + dc * i
if (r < 0 || r >= rows || c < 0 || c >= cols || g[r][c] !== word[i])
break
}
if (i === word.length)
count++
}
}
}
return count
}
export function part2(input: string) {
const g = grid(input)
const rows = g.length
const cols = g[0].length
let count = 0
const check = (chars: string[]): boolean => {
return chars.join('') === 'MAS' || chars.join('') === 'SAM'
}
for (let row = 1; row < rows - 1; row++) {
for (let col = 1; col < cols - 1; col++) {
if (g[row][col] !== 'A')
continue
const [r1, c1, r2, c2] = [row - 1, col - 1, row + 1, col + 1]
const [r3, c3, r4, c4] = [row - 1, col + 1, row + 1, col - 1]
if (r1 < 0 || r2 >= rows || c1 < 0 || c2 >= cols)
continue
if (r3 < 0 || r4 >= rows || c3 < 0 || c4 >= cols)
continue
const diag1 = [g[r1][c1], 'A', g[r2][c2]]
const diag2 = [g[r3][c3], 'A', g[r4][c4]]
if (check(diag1) && check(diag2))
count++
}
}
return count
}