Solve 2025-08

This commit is contained in:
2025-12-08 14:09:00 +01:00
parent 63a7a93aba
commit 5421fde574
6 changed files with 119 additions and 1 deletions
+7
View File
@@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="2025-08" type="BunRunConfiguration">
<option name="program" value="2025/08/run.ts" />
<option name="workingDirectory" value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
</component>
+31
View File
@@ -0,0 +1,31 @@
import { describe, expect, it } from 'bun:test';
import { solveFirst, solveSecond } from './index.ts';
describe('2025-08', () => {
const testInput = `162,817,812
57,618,57
906,360,560
592,479,940
352,342,300
466,668,158
542,29,236
431,825,988
739,650,466
52,470,668
216,146,977
819,987,18
117,168,530
805,96,715
346,949,466
970,615,88
941,993,340
862,61,35
984,92,344
425,690,689`;
it('first', () => {
expect(solveFirst(testInput, 10)).toBe(40);
});
it('second', () => {
expect(solveSecond(testInput)).toBe(25272);
});
});
+51
View File
@@ -0,0 +1,51 @@
import { multBy, uniqueIndexCombinations } from '../../utils/array.ts';
import { euclideanDistanceSqrtless } from '../../utils/coordinates.ts';
export function solveFirst(input: string, count = 1000): number {
const junctions = input
.split('\n')
.map((row) => row.split(',').map(Number) as [number, number, number]);
const edges = uniqueIndexCombinations(junctions)
.map(
({ aI, bI }) =>
[aI, bI, euclideanDistanceSqrtless(junctions[aI], junctions[bI])] as [
number,
number,
number
]
)
.toSorted((a, b) => a[2] - b[2]);
const circuits = junctions.map((_, i) => [i]);
for (let i = 0; i < edges.length; i++) {
const [u, v] = edges[i];
const circuitU = circuits.find((c) => c.includes(u))!;
const circuitV = circuits.find((c) => c.includes(v))!;
if (i === count)
return multBy(
circuits.toSorted((a, b) => b.length - a.length).slice(0, 3),
(e) => e.length
);
// already in same circuit, ignore
if (circuitU === circuitV) continue;
// merge v into u
circuitU.push(...circuitV);
circuits.splice(circuits.indexOf(circuitV), 1);
// end condition for second
if (circuits.length === 1) {
return junctions[u][0] * junctions[v][0];
}
}
return -1;
}
export function solveSecond(input: string): number {
return solveFirst(input, Number.MAX_SAFE_INTEGER);
}
+12
View File
@@ -0,0 +1,12 @@
import { join } from 'path';
import { solveFirst, solveSecond } from './index.ts';
const input = await Bun.file(join(__dirname, 'input.txt')).text();
const firstAnswer = solveFirst(input);
console.log(firstAnswer);
const secondAnswer = solveSecond(input);
console.log(secondAnswer);