diff --git a/solutions/04-b.ts b/solutions/04-b.ts index 9b61ab1..14f95eb 100644 --- a/solutions/04-b.ts +++ b/solutions/04-b.ts @@ -6,13 +6,12 @@ import { readInput } from "../utils.ts"; class DayFour implements Solvable { input = readInput('04') + matrix: string[][] = this.input.trim().split('\n').map(line => line.split('')) + public part1(): Solvable { + const matrix = this.matrix - const transpose = (m: string[][]) => m[0].map((_, i) => m.map(x => x[i])) - - const matrix = this.input.trim().split('\n').map(line => line.split('')) - - function lookAround(x: number, y: number) { + const lookAround = (x: number, y: number) => { const allowed = { top: y >= 3, bottom: y <= matrix.length - 4, @@ -77,6 +76,45 @@ class DayFour implements Solvable { } public part2(): Solvable { + 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) return this } }