-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-4.js
118 lines (107 loc) · 3.09 KB
/
day-4.js
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
import { readFileSync } from "node:fs";
const input = readFileSync("./day-4.txt")
.toString()
.split("\n")
.map((line) => line.split(""));
/**
* Part 1
*/
const XMAS = "XMAS";
let xmasCounter = 0;
for (let i = 0; i < input.length; i++) {
for (let j = 0; j < input[i].length; j++) {
if (input[i][j] !== "X") continue;
else {
// check ahead
if (j <= input[i].length - 4) {
const t = `${input[i][j]}${input[i][j + 1]}${input[i][j + 2]}${
input[i][j + 3]
}`;
if (t === XMAS) xmasCounter++;
}
// check behind
if (j >= 3) {
const t = `${input[i][j]}${input[i][j - 1]}${input[i][j - 2]}${
input[i][j - 3]
}`;
if (t === XMAS) xmasCounter++;
}
// check down
if (i <= input.length - 4) {
const t = `${input[i][j]}${input[i + 1][j]}${input[i + 2][j]}${
input[i + 3][j]
}`;
if (t === XMAS) xmasCounter++;
}
// check up
if (i >= 3) {
const t = `${input[i][j]}${input[i - 1][j]}${input[i - 2][j]}${
input[i - 3][j]
}`;
if (t === XMAS) xmasCounter++;
}
// check top left
if (i >= 3 && j >= 3) {
const t = `${input[i][j]}${input[i - 1][j - 1]}${input[i - 2][j - 2]}${
input[i - 3][j - 3]
}`;
if (t === XMAS) xmasCounter++;
}
// check top right
if (i >= 3 && j <= input[i].length - 4) {
const t = `${input[i][j]}${input[i - 1][j + 1]}${input[i - 2][j + 2]}${
input[i - 3][j + 3]
}`;
if (t === XMAS) xmasCounter++;
}
// check down left
if (i <= input.length - 4 && j >= 3) {
const t = `${input[i][j]}${input[i + 1][j - 1]}${input[i + 2][j - 2]}${
input[i + 3][j - 3]
}`;
if (t === XMAS) xmasCounter++;
}
// check down right
if (i <= input.length - 4 && j <= input[i].length - 4) {
const t = `${input[i][j]}${input[i + 1][j + 1]}${input[i + 2][j + 2]}${
input[i + 3][j + 3]
}`;
if (t === XMAS) xmasCounter++;
}
}
}
}
console.log({ xmasCounter });
/**
* Part 2
*/
const MAS = "MAS";
let masCounter = 0;
for (let i = 0; i < input.length; i++) {
for (let j = 0; j < input[i].length; j++) {
if (
i === 0 ||
j === 0 ||
j === input[i].length - 1 ||
i === input.length - 1
)
continue;
if (input[i][j] !== "A") continue;
else {
const topLeft = `${input[i - 1][j - 1]}`;
const downRight = `${input[i + 1][j + 1]}`;
const topRight = `${input[i - 1][j + 1]}`;
const downLeft = `${input[i + 1][j - 1]}`;
const diagonalOne = `${topLeft}A${downRight}`;
const diagonalOneReversed = diagonalOne.split("").reverse().join("");
const diagonalTwo = `${topRight}A${downLeft}`;
const diagonalTwoReversed = diagonalTwo.split("").reverse().join("");
if (
(diagonalOne === MAS || diagonalOneReversed === MAS) &&
(diagonalTwo === MAS || diagonalTwoReversed === MAS)
)
masCounter++;
}
}
}
console.log({ masCounter });