From acd66b28accb025e23b45da5c2838590403e7625 Mon Sep 17 00:00:00 2001 From: Pascal Syma Date: Wed, 25 Dec 2024 11:31:41 +0100 Subject: [PATCH] Solve 2024-25 --- 2024/25/2024-25.run.xml | 7 ++++++ 2024/25/index.test.ts | 47 +++++++++++++++++++++++++++++++++++++++++ 2024/25/index.ts | 35 ++++++++++++++++++++++++++++++ 2024/25/run.ts | 8 +++++++ 4 files changed, 97 insertions(+) create mode 100644 2024/25/2024-25.run.xml create mode 100644 2024/25/index.test.ts create mode 100644 2024/25/index.ts create mode 100644 2024/25/run.ts diff --git a/2024/25/2024-25.run.xml b/2024/25/2024-25.run.xml new file mode 100644 index 0000000..734cdfd --- /dev/null +++ b/2024/25/2024-25.run.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/2024/25/index.test.ts b/2024/25/index.test.ts new file mode 100644 index 0000000..2fb76d1 --- /dev/null +++ b/2024/25/index.test.ts @@ -0,0 +1,47 @@ +import { describe, expect, it } from 'bun:test'; +import { solveFirst } from './index.ts'; + +describe('2024-25', () => { + it('first', () => { + const testInput = `##### +.#### +.#### +.#### +.#.#. +.#... +..... + +##### +##.## +.#.## +...## +...#. +...#. +..... + +..... +#.... +#.... +#...# +#.#.# +#.### +##### + +..... +..... +#.#.. +###.. +###.# +###.# +##### + +..... +..... +..... +#.... +#.#.. +#.#.# +#####`; + expect(solveFirst(testInput)).toBe(3); + }); +}); diff --git a/2024/25/index.ts b/2024/25/index.ts new file mode 100644 index 0000000..01bccc6 --- /dev/null +++ b/2024/25/index.ts @@ -0,0 +1,35 @@ +import { sumArray, sumBy } from '../../utils/array.ts'; + +type KeyLock = number[]; + +function checkKeyLock(lock: KeyLock, key: KeyLock, max = 5) { + if (lock.length !== key.length) { + return false; + } + return lock.every((d, i) => d + key[i] <= max); +} + +export function solveFirst(input: string): number { + const [keys, locks] = input.split('\n\n').reduce( + (combinations, b) => { + const rows = b.split('\n'); + const isLock = b[0][0] === '#' ? 1 : 0; + const comb: KeyLock = []; + for (let i = 0; i < rows[0].length; i++) { + const column = rows.flatMap((r) => r[i]); + comb.push(column.filter((c) => c === '#').length - 1); + } + combinations[isLock].push(comb); + return combinations; + }, + [[], []] as [KeyLock[], KeyLock[]] + ); + // const locks = blocks.filter((b) => b[0][0] === '#'); + // const key = blocks.filter((b) => b[0][0] === '.'); + + const found = locks.map((l) => + sumBy(keys, (k) => (checkKeyLock(l, k) ? 1 : 0)) + ); + + return sumArray(found); +} diff --git a/2024/25/run.ts b/2024/25/run.ts new file mode 100644 index 0000000..226fb13 --- /dev/null +++ b/2024/25/run.ts @@ -0,0 +1,8 @@ +import { join } from 'path'; +import { solveFirst } from './index.ts'; + +const input = await Bun.file(join(__dirname, 'input.txt')).text(); + +const firstAnswer = solveFirst(input); + +console.log(firstAnswer);