aoc24/solutions/04-a.ts
2024-12-06 21:34:02 +01:00

64 lines
No EOL
1.8 KiB
TypeScript

import { solvables } from "../main.ts";
import type { Solvable } from "../solvable.ts";
import { readInput } from "../utils.ts";
class DayFour implements Solvable {
input = readInput('04')
public part1(): Solvable {
const transpose = (m: string[][]) => m[0].map((_,i) => m.map(x => x[i]))
const matrix = this.input.trim().split('\n').map(line => line.split(''))
const countOccHorizontal = (mat: string[][]) => mat
.map(line => line.join('')
.matchAll(/(XMAS|SAMX)/g)
.map(match => match[0])
.toArray().length)
.reduce((prev, curr) => prev + curr)
// b/y = height
// a/x = width
// start top right
const getDiagonals = (mat: string[][]) => {
let [b,a] = [0, 140 - 1]
const lines = []
while (b < mat.length) {
let line = ''
for (let [y,x] = [b,a]; x < 140 && y < 140; y++,x++)
line += mat[y][x]
lines.push(line.split(''))
if (a > 0) {
a--
} else {
b++
}
}
return lines
}
// console.log(getDiagonals(matrix.reverse()))
console.log(transpose(matrix).map(line=> line.join('')))
const downRight = countOccHorizontal(getDiagonals(matrix))
const downLeft = countOccHorizontal(getDiagonals(matrix.reverse()))
const horizontal = countOccHorizontal(matrix)
const vertical = countOccHorizontal(transpose(matrix))
console.log(horizontal + vertical + downRight + downLeft)
return this
}
public part2(): Solvable {
return this
}
}
solvables.push(new DayFour())