-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.js
55 lines (51 loc) · 1.53 KB
/
solution.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
const fs = require("fs");
const input = fs.readFileSync("input.txt", "utf8").trim().split("\n\n");
let sum = 0;
function transposeMatrix(matrix) {
return matrix[0].map((_, colIndex) => matrix.map((j) => j[colIndex]));
}
function findReflectionIndex(pattern, nonMatching) {
pattern = pattern.map((j) => j.join(""));
for (let i = 0; i < pattern.length - 1; i++) {
let nonMatchingCount = 0;
for (let j = 0; j < pattern.length; j++) {
if (i + 1 + (i - j) >= 0 && i + 1 + (i - j) < pattern.length) {
for (let k = 0; k < pattern[i].length; k++) {
if (pattern[j][k] !== pattern[i + 1 + (i - j)][k]) {
nonMatchingCount++;
}
}
}
}
if (nonMatchingCount === nonMatching) {
return i + 1;
}
}
return null;
}
function calculatePatternValue(pattern, nonMatching) {
pattern = pattern.split("\n").map((row) => row.split(""));
let row = findReflectionIndex(pattern, nonMatching);
if (row !== null) {
return 100 * row;
}
const transposedPattern = transposeMatrix(pattern);
let column = findReflectionIndex(transposedPattern, nonMatching);
if (column !== null) {
return column;
}
return 0;
}
for (const part of ["part1", "part2"]) {
sum = 0;
const start = performance.now();
for (let grid of input) {
if (part === "part1") {
sum += calculatePatternValue(grid, 0);
} else {
sum += calculatePatternValue(grid, 2);
}
}
console.log(part, ": ", sum);
console.log(`Execution time ${part}: ${performance.now() - start} ms`);
}