Solve 2025-12, lol
Bun CI / test (push) Successful in 37s

This commit is contained in:
2025-12-12 08:57:17 +01:00
parent 3e3806c1b9
commit c664361b18
5 changed files with 82 additions and 2 deletions
+7
View File
@@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="2025-12" type="BunRunConfiguration">
<option name="program" value="2025/12/run.ts" />
<option name="workingDirectory" value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
</component>
+41
View File
@@ -0,0 +1,41 @@
import { describe, it } from 'bun:test';
describe('2025-12', () => {
it('first', () => {
const testInput = `0:
###
##.
##.
1:
###
##.
.##
2:
.##
###
##.
3:
##.
###
##.
4:
###
#..
###
5:
###
.#.
###
4x4: 0 0 0 0 2 0
12x5: 1 0 1 0 2 2
12x5: 1 0 1 0 3 2`;
// Doesn't work on test data
// expect(solveFirst(testInput)).toBe(2);
});
});
+24
View File
@@ -0,0 +1,24 @@
import { sumBy } from '../../utils/array.ts';
export function solveFirst(input: string): number {
const rawShapes = input.split('\n\n');
const regions = rawShapes
.pop()!
.split('\n')
.map((row) => {
const [size, count] = row.split(': ');
const [wide, long] = size.split('x').map(Number);
return { wide, long, count: count.split(' ').map(Number) };
});
const shapes = rawShapes.map((s) => s.split('\n').slice(1));
const shapeWeight = shapes.map((s) =>
sumBy(s, (r) => [...r.matchAll(/#/g)].length)
);
// works for the actual data, not for testing lmao
const possible = regions.filter(
(r) => r.long * r.wide >= sumBy(r.count, (e, i) => e * shapeWeight[i])
);
return possible.length;
}
+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);
+2 -2
View File
@@ -14,9 +14,9 @@ export function sumArray(arr: ReadonlyArray<number>): number {
*/
export function sumBy<E>(
arr: ReadonlyArray<E>,
by: (element: E) => number | string
by: (element: E, index: number) => number | string
): number {
return arr.reduce((sum, element) => sum + Number(by(element)), 0);
return arr.reduce((sum, element, i) => sum + Number(by(element, i)), 0);
}
/**