diff --git a/day25.peggy b/day25.peggy new file mode 100644 index 0000000..8510541 --- /dev/null +++ b/day25.peggy @@ -0,0 +1,7 @@ +schemata = (lock / key)|.., "\n"| + +lock = "#####" "\n" rows:row+ { return {type: 'lock', rows, h: []}} + +key = rows:row+ { return { type: 'key', rows: rows.slice(0, -1), h: []}} + +row = @[#.]+ "\n" diff --git a/day25.ts b/day25.ts new file mode 100644 index 0000000..15ff583 --- /dev/null +++ b/day25.ts @@ -0,0 +1,45 @@ +import { type MainArgs, parseFile } from './lib/utils.ts'; + +interface KeyLock { + type: 'key' | 'lock'; + rows: string[][]; + h: number[]; +} +type Parsed = KeyLock[]; + +function fits(key: number[], lock: number[]): boolean { + for (let i = 0; i < 5; i++) { + if (key[i] + lock[i] > 5) { + return false; + } + } + return true; +} + +function part1(inp: Parsed): number { + for (const i of inp) { + for (let col = 0; col < 5; col++) { + let tot = 0; + for (let row = 0; row < i.rows.length; row++) { + if (i.rows[row][col] === '#') { + tot++; + } + } + i.h.push(tot); + } + } + let count = 0; + for (const key of inp.filter(({ type }) => type === 'key')) { + for (const lock of inp.filter(({ type }) => type === 'lock')) { + if (fits(key.h, lock.h)) { + count++; + } + } + } + return count; +} + +export default async function main(args: MainArgs): Promise<[number]> { + const inp = await parseFile(args); + return [part1(inp)]; +} diff --git a/inputs b/inputs index cc618e3..3f1c03b 160000 --- a/inputs +++ b/inputs @@ -1 +1 @@ -Subproject commit cc618e35fc7c313f08404ebb9fb42ab5bce296d5 +Subproject commit 3f1c03bd92fe3f00d58224ba1f83802ddd1de045 diff --git a/test/day25.js b/test/day25.js new file mode 100644 index 0000000..e9e4620 --- /dev/null +++ b/test/day25.js @@ -0,0 +1 @@ +export default [3397];