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 {
|
|
|
|
|
2024-12-07 02:16:43 +01:00
|
|
|
const transpose = (m: string[][]) => m[0].map((_, i) => m.map(x => x[i]))
|
2024-12-06 20:55:20 +01:00
|
|
|
|
2024-12-07 02:16:43 +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('')
|
2024-12-07 02:16:43 +01:00
|
|
|
.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[][]) => {
|
2024-12-07 02:16:43 +01:00
|
|
|
let [b, a] = [0, 140 - 1]
|
2024-12-06 20:55:20 +01:00
|
|
|
const lines = []
|
|
|
|
while (b < mat.length) {
|
|
|
|
let line = ''
|
2024-12-07 02:16:43 +01:00
|
|
|
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()))
|
|
|
|
|
2024-12-07 02:16:43 +01:00
|
|
|
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-07 02:16:43 +01:00
|
|
|
|
2024-12-06 20:55:20 +01:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
public part2(): Solvable {
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
solvables.push(new DayFour())
|