thank you gurke!

This commit is contained in:
mtrx 2024-12-06 21:34:02 +01:00
parent d28803dba6
commit 1d79c15e2c
3 changed files with 88 additions and 5 deletions

View file

@ -1,3 +1,4 @@
{
"deno.enable": true
"deno.enable": true,
"editor.wordWrapColumn": 300
}

View file

@ -24,13 +24,11 @@ class DayFour implements Solvable {
// start top right
const getDiagonals = (mat: string[][]) => {
let [b,a] = [0, mat[0].length - 1]
let [b,a] = [0, 140 - 1]
const lines = []
while (b < mat.length) {
let line = ''
for (let [y,x] = [b,a]; x < mat[y].length && y < mat.length - 1; y++,x++)
for (let [y,x] = [b,a]; x < 140 && y < 140; y++,x++)
line += mat[y][x]
lines.push(line.split(''))

84
solutions/04-b.ts Normal file
View file

@ -0,0 +1,84 @@
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(''))
function lookAround(x: number, y: number) {
const allowed = {
top: y >= 3,
bottom: y <= matrix.length - 4,
left: x >= 3,
right: x <= matrix.length - 4
}
const results = []
if (allowed.top) {
// top
results.push(matrix[y][x] + matrix[y - 1][x] + matrix[y - 2][x] + matrix[y - 3][x])
if (allowed.left) {
// topleft
results.push(matrix[y][x] + matrix[y - 1][x - 1] + matrix[y - 2][x - 2] + matrix[y - 3][x - 3])
}
if (allowed.right) {
// topright
results.push(matrix[y][x] + matrix[y - 1][x + 1] + matrix[y - 2][x + 2] + matrix[y - 3][x + 3])
}
}
if (allowed.bottom) {
// bottom
results.push(matrix[y][x] + matrix[y + 1][x] + matrix[y + 2][x] + matrix[y + 3][x])
if (allowed.left) {
// bottomleft
results.push(matrix[y][x] + matrix[y + 1][x - 1] + matrix[y + 2][x - 2] + matrix[y + 3][x - 3])
}
if (allowed.right) {
// bottomright
results.push(matrix[y][x] + matrix[y + 1][x + 1] + matrix[y + 2][x + 2] + matrix[y + 3][x + 3])
}
}
if (allowed.left) {
// left
results.push(matrix[y][x] + matrix[y][x - 1] + matrix[y][x - 2] + matrix[y][x - 3])
}
if (allowed.right) {
// right
results.push(matrix[y][x] + matrix[y][x + 1] + matrix[y][x + 2] + matrix[y][x + 3])
}
return results
}
function countMatches(resultsArr: string[]): number {
return resultsArr.filter(result => result === 'XMAS').length
}
let amountOfMatches = 0
for (let y = 0; y < matrix.length; y++) {
for (let x = 0; x < matrix.length; x++) {
if (matrix[y][x] === 'X')
amountOfMatches += countMatches(lookAround(x, y))
}
}
console.log(amountOfMatches)
return this
}
public part2(): Solvable {
return this
}
}
solvables.push(new DayFour())