-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.ts
97 lines (84 loc) · 2.16 KB
/
day4.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
const input = await Deno.readTextFile("day4.input");
const grid = input.split("\n");
function part1() {
console.log("part 1");
let solutions = 0;
for (let x = 0; x < grid.length; x++) {
for (let y = 0; y < grid[0].length; y++) {
if (grid[x][y] === "X") {
solutions += countXmasInstancesAtCoordinate(x, y);
}
}
}
console.log(solutions);
}
class WordBuilder {
private word: string;
constructor(private grid: string[], private x: number, private y: number) {
this.grid = grid;
this.x = x;
this.y = y;
if (x >= 0 && x < grid.length && y >= 0 && y < grid[x].length) {
this.word = grid[x].charAt(y);
} else {
this.word = "";
}
}
public navigate(xOffset: number, yOffset: number) {
this.x += xOffset;
this.y += yOffset;
if (
this.x >= 0 &&
this.x < this.grid.length &&
this.y >= 0 &&
this.y < this.grid[this.x].length
) {
this.word += this.grid[this.x][this.y];
}
return this;
}
public getWord() {
return this.word;
}
}
const countXmasInstancesAtCoordinate = (x: number, y: number) => {
let xmasInstances = 0;
for (let xOffset = -1; xOffset <= 1; xOffset++) {
for (let yOffset = -1; yOffset <= 1; yOffset++) {
const check = new WordBuilder(grid, x, y);
for (let i = 0; i < 3; i++) {
check.navigate(xOffset, yOffset);
}
if (check.getWord() === "XMAS") xmasInstances++;
}
}
return xmasInstances;
};
part1();
function part2() {
console.log("part 2");
let solutions = 0;
for (let x = 0; x < grid.length; x++) {
for (let y = 0; y < grid[0].length; y++) {
if (grid[x][y] === "A" && isCrossAtCoordinate(x, y)) {
solutions += 1;
}
}
}
console.log(solutions);
}
function isCrossAtCoordinate(x: number, y: number) {
const firstSlash = new WordBuilder(grid, x - 1, y - 1)
.navigate(1, 1)
.navigate(1, 1)
.getWord();
const secondSlash = new WordBuilder(grid, x - 1, y + 1)
.navigate(1, -1)
.navigate(1, -1)
.getWord();
return (
(firstSlash === "MAS" || firstSlash === "SAM") &&
(secondSlash === "MAS" || secondSlash === "SAM")
);
}
part2();