-
Notifications
You must be signed in to change notification settings - Fork 0
/
day13_2.js
93 lines (87 loc) · 2.48 KB
/
day13_2.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
const { parse } = require('path');
const lineReader = require('readline').createInterface({
input: require('fs').createReadStream('input.txt'),
});
let pattern = [];
const patterns = [];
lineReader.on('line', function (line) {
if (line.length === 0) {
patterns.push(pattern);
pattern = [];
return;
}
const lineArr = line.split('');
pattern.push(lineArr);
});
function findReflectionLines(p) {
const rows = [];
for (let row = 1; row < p.length; row++) {
let reflection = true;
let offset = 1;
while (row - offset >= 0 && row + offset - 1 < p.length) {
for (let col = 0; col < p[0].length; col++) {
// console.log('row ' + row);
// console.log(fileData[row][col - offset] + ' ' + fileData[row][col + offset]);
if (p[row - offset][col] !== p[row + offset - 1][col]) {
reflection = false;
break;
}
}
if (!reflection) {
break;
}
offset++;
}
if (reflection) {
rows.push(row);
}
}
return rows;
}
lineReader.on('close', function () {
patterns.push(pattern);
const rows = [];
const columns = [];
patterns.forEach((p) => {
const set = new Set();
for (let col = 1; col < p[0].length; col++) {
// console.log('col ' + col);
let mismatchCount = 0;
let offset = 1;
while (col - offset >= 0 && col + offset - 1 < p[0].length) {
for (let row = 0; row < p.length; row++) {
// console.log('row ' + row);
// console.log(fileData[row][col - offset] + ' ' + fileData[row][col + offset]);
if (p[row][col - offset] !== p[row][col + offset - 1]) {
mismatchCount++;
}
}
offset++;
}
if (mismatchCount === 1) {
columns.push(col);
}
}
for (let row = 1; row < p.length; row++) {
let offset = 1;
let mismatchCount = 0;
while (row - offset >= 0 && row + offset <= p.length) {
for (let col = 0; col < p[0].length; col++) {
// console.log('row ' + row);
// console.log(fileData[row][col - offset] + ' ' + fileData[row][col + offset]);
if (p[row - offset][col] !== p[row + offset - 1][col]) {
mismatchCount++;
}
}
offset++;
}
if (mismatchCount === 1) {
rows.push(row);
}
}
console.log(rows);
});
let sum = columns.reduce((prev, col) => col + prev, 0);
sum += rows.reduce((prev, row) => row * 100 + prev, 0);
console.log(sum);
});