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
+7
View File
@@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="2024-25" type="BunRunConfiguration">
<option name="program" value="2024/25/run.ts" />
<option name="workingDirectory" value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
</component>
+47
View File
@@ -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);
});
});
+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);
}
+8
View File
@@ -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);