diff --git a/2024/day/25/part/1/solve copy.ts b/2024/day/25/part/1/solve copy.ts new file mode 100644 index 0000000..4be765c --- /dev/null +++ b/2024/day/25/part/1/solve copy.ts @@ -0,0 +1,29 @@ +export default function solve(input: string) { + const locks: number[][] = []; + const keys: number[][] = []; + for (const lines of input.split("\n\n")) { + const rows = lines.split("\n").slice(1, -1); + const heights = Array.from(rows[0], () => 0); + if (lines.startsWith(".")) rows.reverse(); + for (let x = 0; x < rows[0].length; x++) { + for (let height = rows.length; height > 0; height--) { + if (rows[height - 1][x] === ".") continue; + heights[x] = height; + break; + } + } + if (lines.startsWith("#")) locks.push(heights); + else keys.push(heights); + } + let count = 0; + for (const lock of locks) { + for (const key of keys) { + if ( + key.every((height, index, { length: max }) => + height + lock[index] <= max + ) + ) count++; + } + } + return count; +} diff --git a/2024/day/25/part/1/solve.test.ts b/2024/day/25/part/1/solve.test.ts new file mode 100644 index 0000000..f6bfcb1 --- /dev/null +++ b/2024/day/25/part/1/solve.test.ts @@ -0,0 +1,48 @@ +import solve from "./solve.ts"; + +import { assertEquals } from "@std/assert"; + +Deno.test("example", () => { + const input = `\ +##### +.#### +.#### +.#### +.#.#. +.#... +..... + +##### +##.## +.#.## +...## +...#. +...#. +..... + +..... +#.... +#.... +#...# +#.#.# +#.### +##### + +..... +..... +#.#.. +###.. +###.# +###.# +##### + +..... +..... +..... +#.... +#.#.. +#.#.# +#####`; + + assertEquals(solve(input), 3); +}); diff --git a/2024/day/25/part/1/solve.ts b/2024/day/25/part/1/solve.ts new file mode 100644 index 0000000..e6b89aa --- /dev/null +++ b/2024/day/25/part/1/solve.ts @@ -0,0 +1,19 @@ +export default function solve(input: string, { size = 5 } = {}) { + const locks: number[][] = []; + const keys: number[][] = []; + for (const schematic of input.split("\n\n")) { + const rows = schematic.split("\n").slice(1, -1); + const heights = Array.from( + { length: size }, + (_, x) => rows.reduce((height, row) => height + +(row[x] === "#"), 0), + ); + (schematic[0] === "#" ? locks : keys).push(heights); + } + let count = 0; + for (const lock of locks) { + for (const key of keys) { + if (key.every((height, index) => height + lock[index] <= size)) count++; + } + } + return count; +}