41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
const fs = require('fs');
|
|
|
|
let input = fs.readFileSync(__dirname + '/input.txt', 'utf8');
|
|
|
|
const vertical: Array<string> = input.split("\n");
|
|
const width = vertical[0].length;
|
|
const height = vertical.length;
|
|
const horizontal: Array<string> = [];
|
|
const diagonalRight: Array<string> = [];
|
|
const diagonalLeft: Array<string> = [];
|
|
|
|
for (let j = 0; j < height; j++) {
|
|
for (let i = 0; i < width; i++) {
|
|
if (!horizontal[i]) {
|
|
horizontal[i] = '';
|
|
}
|
|
horizontal[i] += vertical[j][i];
|
|
const leftIndex = j+i;
|
|
if (!diagonalLeft[leftIndex]) {
|
|
diagonalLeft[leftIndex] = '';
|
|
}
|
|
diagonalLeft[leftIndex] += vertical[j][i];
|
|
const rightIndex = j - i + (Math.max(width, height)) - 1;
|
|
if (!diagonalRight[rightIndex]) {
|
|
diagonalRight[rightIndex] = '';
|
|
}
|
|
diagonalRight[rightIndex] += vertical[j][i];
|
|
}
|
|
};
|
|
|
|
let result = 0;
|
|
|
|
[vertical, horizontal, diagonalLeft, diagonalRight].forEach((arr) => {
|
|
arr.forEach((string) => {
|
|
const fwMatches = string.matchAll(/XMAS/g);
|
|
const bwMatches = string.matchAll(/SAMX/g);
|
|
result += Array.from(fwMatches).length + Array.from(bwMatches).length;
|
|
});
|
|
});
|
|
|
|
console.log(result); |