2024-12-06 21:34:02 +01:00
|
|
|
import { solvables } from "../main.ts";
|
|
|
|
|
|
|
|
import type { Solvable } from "../solvable.ts";
|
|
|
|
import { readInput } from "../utils.ts";
|
|
|
|
|
|
|
|
class DayFour implements Solvable {
|
|
|
|
input = readInput('04')
|
|
|
|
|
2024-12-06 21:55:19 +01:00
|
|
|
matrix: string[][] = this.input.trim().split('\n').map(line => line.split(''))
|
2024-12-06 21:34:02 +01:00
|
|
|
|
2024-12-06 21:55:19 +01:00
|
|
|
public part1(): Solvable {
|
|
|
|
const matrix = this.matrix
|
2024-12-06 21:34:02 +01:00
|
|
|
|
2024-12-06 21:55:19 +01:00
|
|
|
const lookAround = (x: number, y: number) => {
|
2024-12-06 21:34:02 +01:00
|
|
|
const allowed = {
|
|
|
|
top: y >= 3,
|
|
|
|
bottom: y <= matrix.length - 4,
|
|
|
|
left: x >= 3,
|
|
|
|
right: x <= matrix.length - 4
|
|
|
|
}
|
|
|
|
|
|
|
|
const results = []
|
|
|
|
if (allowed.top) {
|
|
|
|
// top
|
|
|
|
results.push(matrix[y][x] + matrix[y - 1][x] + matrix[y - 2][x] + matrix[y - 3][x])
|
|
|
|
if (allowed.left) {
|
|
|
|
// topleft
|
|
|
|
results.push(matrix[y][x] + matrix[y - 1][x - 1] + matrix[y - 2][x - 2] + matrix[y - 3][x - 3])
|
|
|
|
}
|
|
|
|
if (allowed.right) {
|
|
|
|
// topright
|
|
|
|
results.push(matrix[y][x] + matrix[y - 1][x + 1] + matrix[y - 2][x + 2] + matrix[y - 3][x + 3])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (allowed.bottom) {
|
|
|
|
// bottom
|
|
|
|
results.push(matrix[y][x] + matrix[y + 1][x] + matrix[y + 2][x] + matrix[y + 3][x])
|
|
|
|
if (allowed.left) {
|
|
|
|
// bottomleft
|
|
|
|
results.push(matrix[y][x] + matrix[y + 1][x - 1] + matrix[y + 2][x - 2] + matrix[y + 3][x - 3])
|
|
|
|
}
|
|
|
|
if (allowed.right) {
|
|
|
|
// bottomright
|
|
|
|
results.push(matrix[y][x] + matrix[y + 1][x + 1] + matrix[y + 2][x + 2] + matrix[y + 3][x + 3])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (allowed.left) {
|
|
|
|
// left
|
|
|
|
results.push(matrix[y][x] + matrix[y][x - 1] + matrix[y][x - 2] + matrix[y][x - 3])
|
|
|
|
}
|
|
|
|
if (allowed.right) {
|
|
|
|
// right
|
|
|
|
results.push(matrix[y][x] + matrix[y][x + 1] + matrix[y][x + 2] + matrix[y][x + 3])
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
function countMatches(resultsArr: string[]): number {
|
|
|
|
return resultsArr.filter(result => result === 'XMAS').length
|
|
|
|
}
|
|
|
|
|
|
|
|
let amountOfMatches = 0
|
|
|
|
for (let y = 0; y < matrix.length; y++) {
|
|
|
|
for (let x = 0; x < matrix.length; x++) {
|
|
|
|
if (matrix[y][x] === 'X')
|
|
|
|
amountOfMatches += countMatches(lookAround(x, y))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(amountOfMatches)
|
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
public part2(): Solvable {
|
2024-12-06 21:55:19 +01:00
|
|
|
const matrix = this.matrix
|
|
|
|
|
|
|
|
const checkMas = (x: number, y: number): boolean => {
|
|
|
|
const allowed = (
|
|
|
|
y >= 1 &&
|
|
|
|
y <= matrix.length - 2 &&
|
|
|
|
x >= 1 &&
|
|
|
|
x <= matrix.length - 2
|
|
|
|
)
|
|
|
|
|
|
|
|
if (allowed) {
|
|
|
|
return (
|
|
|
|
(
|
|
|
|
matrix[y - 1][x - 1] === 'M' &&
|
|
|
|
matrix[y + 1][x + 1] === 'S' ||
|
|
|
|
matrix[y - 1][x - 1] === 'S' &&
|
|
|
|
matrix[y + 1][x + 1] === 'M'
|
|
|
|
)
|
|
|
|
&&
|
|
|
|
(
|
|
|
|
matrix[y - 1][x + 1] === 'M' &&
|
|
|
|
matrix[y + 1][x - 1] === 'S' ||
|
|
|
|
matrix[y - 1][x + 1] === 'S' &&
|
|
|
|
matrix[y + 1][x - 1] === 'M'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
let amountOfMatches = 0
|
|
|
|
for (let y = 0; y < matrix.length; y++) {
|
|
|
|
for (let x = 0; x < matrix.length; x++) {
|
|
|
|
if (matrix[y][x] === 'A')
|
|
|
|
if (checkMas(x, y)) amountOfMatches += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(amountOfMatches)
|
2024-12-06 21:34:02 +01:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
solvables.push(new DayFour())
|