From 795d140ba4822f93fee1a736d9de54d4b04f8188 Mon Sep 17 00:00:00 2001 From: Pascal Syma Date: Sun, 8 Dec 2024 20:05:00 +0100 Subject: [PATCH] Cleanup on 2024-08 --- .prettierignore | 4 +--- 2024/08/index.ts | 57 ++++++++++++++++++-------------------------- utils/coordinates.ts | 1 + 3 files changed, 25 insertions(+), 37 deletions(-) diff --git a/.prettierignore b/.prettierignore index 834d85f..1395b20 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,4 +1,2 @@ -.output -.nuxt node_modules -prisma/migrations \ No newline at end of file +input.txt diff --git a/2024/08/index.ts b/2024/08/index.ts index 1d7b89c..4545072 100644 --- a/2024/08/index.ts +++ b/2024/08/index.ts @@ -1,26 +1,32 @@ import { combinations, unique } from '../../utils/array.ts'; -import { inBounds, isCoordinateEqual } from '../../utils/coordinates.ts'; +import { + type Coordinates, + inBounds, + isCoordinateEqual +} from '../../utils/coordinates.ts'; -export function solveFirst(input: string): number { - const rows = input.split('\n'); - - const antennas = new Map(); +export function groupCoordinates(rows: string[]) { + const antennas = new Map(); for (let y = 0; y < rows.length; y++) { for (let x = 0; x < rows[y].length; x++) { - const digit = rows[y][x]; - if (digit === '.') { + const char = rows[y][x]; + if (char === '.') { continue; } - const entry = antennas.get(digit) || []; + const entry = antennas.get(char) || []; entry.push([x, y]); - antennas.set(digit, entry); + antennas.set(char, entry); } } - const antiNodes: [number, number][] = []; + return antennas; +} + +export function solveFirst(input: string): number { + const rows = input.split('\n'); + const antennas = groupCoordinates(rows); + const antiNodes: Coordinates = []; - // console.log(rows); - // console.log(antennas); antennas.forEach((positions) => { combinations(positions).forEach(({ a, b }) => antiNodes.push([a[0] - (b[0] - a[0]), a[1] - (b[1] - a[1])]) @@ -31,36 +37,18 @@ export function solveFirst(input: string): number { .filter(inBounds(rows[0].length, rows.length)) .filter(unique(isCoordinateEqual)); - // validAntiNodes.forEach(([x, y]) => rows[y][x] === '.' && replaceChar(rows, x, y, '#')); - // - // console.log(rows.join('\n')); return validAntiNodes.length; } export function solveSecond(input: string): number { const rows = input.split('\n'); - const antennas = new Map(); - - const yMax = rows.length; - const xMax = rows[0].length; - - for (let y = 0; y < yMax; y++) { - for (let x = 0; x < xMax; x++) { - const digit = rows[y][x]; - if (digit === '.') { - continue; - } - const entry = antennas.get(digit) || []; - entry.push([x, y]); - antennas.set(digit, entry); - } - } - const antiNodes: [number, number][] = []; + const antennas = groupCoordinates(rows); + const antiNodes: Coordinates = []; antennas.forEach((positions) => { combinations(positions).forEach(({ a, b }) => { - for (let y = 0; y < yMax; y++) { + for (let y = 0; y < rows.length; y++) { antiNodes.push([a[0] + (b[0] - a[0]) * y, a[1] + (b[1] - a[1]) * y]); } }); @@ -70,7 +58,8 @@ export function solveSecond(input: string): number { .filter(inBounds(rows[0].length, rows.length)) .filter(unique(isCoordinateEqual)); - // console.log(validAntiNodes); + // Show the anti-nodes on the input map + // // validAntiNodes.forEach( // ([x, y]) => rows[y][x] === '.' && replaceChar(rows, x, y, '#') // ); diff --git a/utils/coordinates.ts b/utils/coordinates.ts index 442560f..5a054e6 100644 --- a/utils/coordinates.ts +++ b/utils/coordinates.ts @@ -1,4 +1,5 @@ export type Coordinate = [number, number]; +export type Coordinates = Coordinate[]; /** * Returns a filter lambda to be used in array.filter() on a xy-Coordinate array (array of two number arrays).