diff --git a/2025/11/2025-11.run.xml b/2025/11/2025-11.run.xml new file mode 100644 index 0000000..9ddf159 --- /dev/null +++ b/2025/11/2025-11.run.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/2025/11/index.test.ts b/2025/11/index.test.ts new file mode 100644 index 0000000..e4e283a --- /dev/null +++ b/2025/11/index.test.ts @@ -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); + }); +}); diff --git a/2025/11/index.ts b/2025/11/index.ts new file mode 100644 index 0000000..ed1fe79 --- /dev/null +++ b/2025/11/index.ts @@ -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, + memo: Map = new Map() +) { + 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; +} diff --git a/2025/11/run.ts b/2025/11/run.ts new file mode 100644 index 0000000..88783cd --- /dev/null +++ b/2025/11/run.ts @@ -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);