Skip to content

Commit

Permalink
Merge pull request #49 from hildjj/day25
Browse files Browse the repository at this point in the history
Day 25
  • Loading branch information
hildjj authored Dec 25, 2024
2 parents 2f7fce7 + 39a9d7c commit b7405a3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
7 changes: 7 additions & 0 deletions day25.peggy
Original file line number Diff line number Diff line change
@@ -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"
45 changes: 45 additions & 0 deletions day25.ts
Original file line number Diff line number Diff line change
@@ -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<Parsed>(args);
return [part1(inp)];
}
2 changes: 1 addition & 1 deletion inputs
1 change: 1 addition & 0 deletions test/day25.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default [3397];

0 comments on commit b7405a3

Please sign in to comment.