Solve 2024-25

This commit is contained in:
2024-12-25 11:31:41 +01:00
parent a09f9ff219
commit acd66b28ac
4 changed files with 97 additions and 0 deletions
+35
View File
@@ -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);
}