aoc24/solutions/04-a.ts

64 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-12-06 20:55:20 +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')
public part1(): Solvable {
const transpose = (m: string[][]) => m[0].map((_, i) => m.map(x => x[i]))
2024-12-06 20:55:20 +01:00
const matrix = this.input.split('\n').map(line => line.split(''))
2024-12-06 20:55:20 +01:00
const countOccHorizontal = (mat: string[][]) => mat
.map(line => line.join('')
.matchAll(/(XMAS|SAMX)/g)
.map(match => match[0])
.toArray().length)
2024-12-06 20:55:20 +01:00
.reduce((prev, curr) => prev + curr)
// b/y = height
// a/x = width
// start top right
const getDiagonals = (mat: string[][]) => {
let [b, a] = [0, 140 - 1]
2024-12-06 20:55:20 +01:00
const lines = []
while (b < mat.length) {
let line = ''
for (let [y, x] = [b, a]; x < 140 && y < 140; y++, x++)
2024-12-06 20:55:20 +01:00
line += mat[y][x]
2024-12-06 20:58:42 +01:00
2024-12-06 20:55:20 +01:00
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('')))
2024-12-06 20:55:20 +01:00
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)
2024-12-06 20:55:20 +01:00
return this
}
public part2(): Solvable {
return this
}
}
solvables.push(new DayFour())