-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday04.ts
More file actions
80 lines (69 loc) · 1.73 KB
/
day04.ts
File metadata and controls
80 lines (69 loc) · 1.73 KB
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
import { readInput, slidingWindow } from "./util.ts";
if (import.meta.main) {
const input = await readInput();
console.log("Part 1:", part1(input));
console.log("Part 2:", part2(input));
}
function part1(input: string): number {
const grid = input.split("\n");
let count = 0;
// Vertical
for (const window of slidingWindow(grid, 1, 4)) {
const str = window.join("");
if (str === "XMAS" || str === "SAMX") count++;
}
// Horizontal
for (const window of slidingWindow(grid, 4, 1)) {
if (window[0] === "XMAS" || window[0] === "SAMX") count++;
}
// Diagonals
for (const win of slidingWindow(grid, 4, 4)) {
const diag1 = win.map((row, i) => row[i]).join("");
const diag2 = win.map((row, i) => row[row.length - i - 1]).join("");
if (diag1 === "XMAS" || diag1 === "SAMX") count++;
if (diag2 === "XMAS" || diag2 === "SAMX") count++;
}
return count;
}
function part2(input: string): number {
const grid = input.split("\n");
let count = 0;
for (const win of slidingWindow(grid, 3, 3)) {
const diag1 = win.map((row, i) => row[i]).join("");
const diag2 = win.map((row, i) => row[row.length - i - 1]).join("");
if (
(diag1 === "MAS" || diag1 === "SAM") &&
(diag2 === "MAS" || diag2 === "SAM")
)
count++;
}
return count;
}
// Test
Deno.test("Part 1: ", () => {
const out = part1(`MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX`);
assertEquals(out, 18);
});
Deno.test("Part 2: ", () => {
const out = part2(`MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX`);
assertEquals(out, 9);
});