-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
183 lines (167 loc) · 3.67 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import run from "aocrunner";
type Input = {
grid: string[][];
rows: number;
cols: number;
};
type Direction = {
r: -1 | 0 | 1;
c: -1 | 0 | 1;
};
type Coord = { r: number; c: number };
type Vector = {
start: Coord;
direction: Direction;
};
const SIDE_DIRECTIONS = [
{ r: -1, c: 0 },
{ r: 0, c: -1 },
{ r: 0, c: 1 },
{ r: 1, c: 0 },
] as const satisfies Direction[];
const DIAGONAL_DIRECTIONS = [
{ r: -1, c: -1 },
{ r: -1, c: 1 },
{ r: 1, c: -1 },
{ r: 1, c: 1 },
] as const satisfies Direction[];
const DIRECTIONS = [
...SIDE_DIRECTIONS,
...DIAGONAL_DIRECTIONS,
] as const satisfies Direction[];
const parseInput = (rawInput: string): Input => {
const grid = rawInput.split("\n").map((line) => line.split(""));
const rows = grid.length;
const cols = grid[0].length;
return { grid, rows, cols };
};
const getOtherMASVector = (original: Vector): Vector => ({
start: {
r: original.start.r + 2 * original.direction.r,
c: original.start.c,
},
direction: {
r: (-1 * original.direction.r) as -1 | 0 | 1,
c: original.direction.c as -1 | 0 | 1,
},
});
const getWord = (
input: Input,
start: Coord,
direction: Direction,
length: number,
): string => {
return Array.from({ length })
.map((_, i) => ({
r: start.r + i * direction.r,
c: start.c + i * direction.c,
}))
.map((coord) => input.grid[coord.r]?.[coord.c])
.join("");
};
const countXMAS = (input: Input): number => {
let count = 0;
for (let r = 0; r < input.rows; r++) {
for (let c = 0; c < input.cols; c++) {
if (input.grid[r][c] === "X") {
DIRECTIONS.forEach((direction) => {
const word = getWord(input, { r, c }, direction, 4);
if (word === "XMAS") count++;
});
}
}
}
return count;
};
const countMASInShapeOfX = (input: Input): number => {
const centerCoords: Coord[] = [];
for (let r = 0; r < input.rows; r++) {
for (let c = 0; c < input.cols; c++) {
if (input.grid[r][c] === "M") {
DIAGONAL_DIRECTIONS.forEach((direction) => {
const word = getWord(input, { r, c }, direction, 3);
if (word === "MAS") {
const otherVector = getOtherMASVector({
start: { r, c },
direction,
});
const otherWord = getWord(
input,
otherVector.start,
otherVector.direction,
3,
);
if (otherWord === "MAS" || otherWord === "SAM") {
const center = {
r: r + direction.r,
c: c + direction.c,
};
if (
!centerCoords.find(
(coord) => coord.r === center.r && coord.c === center.c,
)
) {
centerCoords.push(center);
}
}
}
});
}
}
}
return centerCoords.length;
};
const part1 = (rawInput: string) => {
const input = parseInput(rawInput);
const count = countXMAS(input);
return count.toString();
};
const part2 = (rawInput: string) => {
const input = parseInput(rawInput);
const count = countMASInShapeOfX(input);
return count.toString();
};
run({
part1: {
tests: [
{
input: `
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
`,
expected: "18",
},
],
solution: part1,
},
part2: {
tests: [
{
input: `
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
`,
expected: "9",
},
],
solution: part2,
},
trimTestInputs: true,
// onlyTests: true,
});