despair day 4
This commit is contained in:
parent
75d541877d
commit
199303ce82
2 changed files with 208 additions and 0 deletions
68
solutions/04.ts
Normal file
68
solutions/04.ts
Normal file
|
@ -0,0 +1,68 @@
|
|||
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, mat[0].length - 1]
|
||||
const lines = []
|
||||
while (b < mat.length) {
|
||||
let [y,x] = [b,a]
|
||||
let line = ''
|
||||
// walk top left of diagonal to bottom right
|
||||
while (x < mat[y].length && y < mat.length - 1) {
|
||||
line += mat[y][x]
|
||||
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())
|
Loading…
Add table
Add a link
Reference in a new issue