Solve 2025-11
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="2025-11" type="BunRunConfiguration">
|
||||
<option name="program" value="2025/11/run.ts" />
|
||||
<option name="workingDirectory" value="$PROJECT_DIR$" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
@@ -0,0 +1,34 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
import { solveFirst, solveSecond } from './index.ts';
|
||||
|
||||
describe('2025-11', () => {
|
||||
it('first', () => {
|
||||
const testInput = `aaa: you hhh
|
||||
you: bbb ccc
|
||||
bbb: ddd eee
|
||||
ccc: ddd eee fff
|
||||
ddd: ggg
|
||||
eee: out
|
||||
fff: out
|
||||
ggg: out
|
||||
hhh: ccc fff iii
|
||||
iii: out`;
|
||||
expect(solveFirst(testInput)).toBe(5);
|
||||
});
|
||||
it('second', () => {
|
||||
const testInput = `svr: aaa bbb
|
||||
aaa: fft
|
||||
fft: ccc
|
||||
bbb: tty
|
||||
tty: ccc
|
||||
ccc: ddd eee
|
||||
ddd: hub
|
||||
hub: fff
|
||||
eee: dac
|
||||
dac: fff
|
||||
fff: ggg hhh
|
||||
ggg: out
|
||||
hhh: out`;
|
||||
expect(solveSecond(testInput)).toBe(2);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
import { sumArray } from '../../utils/array.ts';
|
||||
|
||||
function parseInput(input: string) {
|
||||
return input.split('\n').map((row) => {
|
||||
const [from, _to] = row.split(': ');
|
||||
|
||||
return { from, to: _to.split(' ') };
|
||||
});
|
||||
}
|
||||
|
||||
function dfs(
|
||||
start: string,
|
||||
target: string,
|
||||
graph: ReturnType<typeof parseInput>,
|
||||
memo: Map<string, number> = new Map<string, number>()
|
||||
) {
|
||||
if (start === target) {
|
||||
return 1;
|
||||
}
|
||||
if (memo.has(start)) {
|
||||
return memo.get(start)!;
|
||||
}
|
||||
const val = sumArray(
|
||||
graph
|
||||
.find((r) => r.from === start)
|
||||
?.to?.map((to) => dfs(to, target, graph, memo)) || [0]
|
||||
);
|
||||
memo.set(start, val);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
export function solveFirst(input: string): number {
|
||||
const rows = parseInput(input);
|
||||
|
||||
return dfs('you', 'out', rows);
|
||||
}
|
||||
|
||||
export function solveSecond(input: string): number {
|
||||
const rows = parseInput(input);
|
||||
|
||||
const svr_fft = dfs('svr', 'fft', rows);
|
||||
const fft_dac = dfs('fft', 'dac', rows);
|
||||
const dac_out = dfs('dac', 'out', rows);
|
||||
|
||||
const svr_dac = dfs('svr', 'dac', rows);
|
||||
const dac_fft = dfs('dac', 'fft', rows);
|
||||
const fft_out = dfs('fft', 'out', rows);
|
||||
|
||||
return svr_fft * fft_dac * dac_out + svr_dac * dac_fft * fft_out;
|
||||
}
|
||||
@@ -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);
|
||||
Reference in New Issue
Block a user