Day 4
This commit is contained in:
41
2024/04/index.ts
Normal file
41
2024/04/index.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
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);
|
||||
28
2024/04/index2.ts
Normal file
28
2024/04/index2.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
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;
|
||||
|
||||
let result = 0;
|
||||
|
||||
for (let j = 1; j < height - 1; j++) {
|
||||
for (let i = 1; i < width - 1; i++) {
|
||||
const letter = vertical[i][j];
|
||||
if (letter !== 'A') {
|
||||
continue;
|
||||
}
|
||||
const diagonalRight = vertical[i-1][j-1] + 'A' + vertical[i+1][j+1];
|
||||
const diagonalLeft = vertical[i-1][j+1] + 'A' + vertical[i+1][j-1];
|
||||
if (
|
||||
(diagonalLeft === 'MAS' || diagonalLeft === 'SAM')
|
||||
&& (diagonalRight === 'MAS' || diagonalRight === 'SAM')
|
||||
) {
|
||||
result++;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
console.log(result);
|
||||
Reference in New Issue
Block a user