From 1d79c15e2c97c16c3c2b5776a61912adcdd0ce3f Mon Sep 17 00:00:00 2001 From: mtrx Date: Fri, 6 Dec 2024 21:34:02 +0100 Subject: [PATCH] thank you gurke! --- .vscode/settings.json | 3 +- solutions/{04.ts => 04-a.ts} | 6 +-- solutions/04-b.ts | 84 ++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 5 deletions(-) rename solutions/{04.ts => 04-a.ts} (92%) create mode 100644 solutions/04-b.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index b943dbc..a1b822a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "deno.enable": true + "deno.enable": true, + "editor.wordWrapColumn": 300 } \ No newline at end of file diff --git a/solutions/04.ts b/solutions/04-a.ts similarity index 92% rename from solutions/04.ts rename to solutions/04-a.ts index f3e3379..26d9071 100644 --- a/solutions/04.ts +++ b/solutions/04-a.ts @@ -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('')) diff --git a/solutions/04-b.ts b/solutions/04-b.ts new file mode 100644 index 0000000..9b61ab1 --- /dev/null +++ b/solutions/04-b.ts @@ -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()) \ No newline at end of file