-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.ts
74 lines (65 loc) · 1.51 KB
/
day04.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
import type { Day } from './Day.ts';
import { diagNeighbors, enumGrid, isInGrid, neighbors } from './utils.ts';
export class DayImpl implements Day {
private readonly input: Array<string[]>;
constructor(input: string) {
this.input = this.parseInput(input);
}
parseInput(input: string) {
return input
.trim()
.split('\n')
.map((line: string) => {
return line
.split('');
});
}
partOne() {
let count = 0;
for (const { x: i, y: j, cell } of enumGrid(this.input)) {
if (cell !== 'X') {
continue;
}
for (const [y, x] of neighbors) {
const word: string[] = ['X'];
for (let k = 1; k < 4; k++) {
const y2 = j + y * k;
const x2 = i + x * k;
if (isInGrid(this.input, y2, x2)) {
word.push(this.input[y2][x2]);
}
}
if (word.join('') === 'XMAS') {
count++;
}
}
}
return count;
}
partTwo() {
const valid = [
'MMSS',
'SSMM',
'MSSM',
'SMMS',
];
let count = 0;
for (const { x: i, y: j, cell } of enumGrid(this.input)) {
if (cell !== 'A') {
continue;
}
const adj: string[] = [];
for (const [y, x] of diagNeighbors) {
const x2 = i + x;
const y2 = j + y;
if (isInGrid(this.input, y2, x2)) {
adj.push(this.input[y2][x2]);
}
}
if (valid.includes(adj.join(''))) {
count++;
}
}
return count;
}
}