Solve 2025-07
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="2025-07" type="BunRunConfiguration">
|
||||
<option name="program" value="2025/07/run.ts" />
|
||||
<option name="workingDirectory" value="$PROJECT_DIR$" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
@@ -0,0 +1,27 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
import { solveFirst, solveSecond } from './index.ts';
|
||||
|
||||
describe('2025-07', () => {
|
||||
const testInput = `.......S.......
|
||||
...............
|
||||
.......^.......
|
||||
...............
|
||||
......^.^......
|
||||
...............
|
||||
.....^.^.^.....
|
||||
...............
|
||||
....^.^...^....
|
||||
...............
|
||||
...^.^...^.^...
|
||||
...............
|
||||
..^...^.....^..
|
||||
...............
|
||||
.^.^.^.^.^...^.
|
||||
...............`;
|
||||
it('first', () => {
|
||||
expect(solveFirst(testInput)).toBe(21);
|
||||
});
|
||||
it('second', () => {
|
||||
expect(solveSecond(testInput)).toBe(40);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
import { MultiMap } from '../../utils/multi-map.ts';
|
||||
|
||||
export function solveFirst(input: string): number {
|
||||
const rows = input.split('\n');
|
||||
const start = rows[0].indexOf('S');
|
||||
|
||||
const beams = new Set([start]);
|
||||
|
||||
let splits = 0;
|
||||
for (let y = 1; y < rows.length; y++) {
|
||||
const newBeams: number[] = [];
|
||||
beams.forEach((beam) => {
|
||||
if (rows[y][beam] === '.' || beam >= rows[y].length) {
|
||||
return;
|
||||
}
|
||||
if (rows[y][beam] === '^') {
|
||||
splits++;
|
||||
beams.delete(beam);
|
||||
newBeams.push(beam - 1);
|
||||
newBeams.push(beam + 1);
|
||||
}
|
||||
});
|
||||
newBeams.forEach((b) => beams.add(b));
|
||||
}
|
||||
|
||||
return splits;
|
||||
}
|
||||
|
||||
export function solveSecond(input: string): number {
|
||||
const rows = input.split('\n');
|
||||
const start = rows[0].indexOf('S');
|
||||
|
||||
return solveDown(rows, 1, start);
|
||||
}
|
||||
|
||||
const cache = new MultiMap<number, [number, number]>();
|
||||
function solveDown(map: string[], startY: number, startX: number): number {
|
||||
if (cache.multiHas(startY, startX)) {
|
||||
return cache.multiGet(startY, startX)!;
|
||||
}
|
||||
for (let y = startY; y < map.length; y++) {
|
||||
if (map[y][startX] === '^') {
|
||||
return cache.multiSet(
|
||||
solveDown(map, y + 1, startX - 1) + solveDown(map, y + 1, startX + 1),
|
||||
startY,
|
||||
startX
|
||||
);
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@@ -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);
|
||||
@@ -11,6 +11,18 @@ export class MultiMap<V, K extends unknown[]> extends Map<K[0], any> {
|
||||
}
|
||||
return this.get(firstKey) as V;
|
||||
}
|
||||
multiHas(...keys: K): boolean {
|
||||
const firstKey = keys[0];
|
||||
const otherKeys = keys.slice(1);
|
||||
if (!this.has(firstKey)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (otherKeys.length > 0) {
|
||||
return (this.get(firstKey)! as MultiMap<any, any>).multiHas(...otherKeys);
|
||||
}
|
||||
return this.has(firstKey);
|
||||
}
|
||||
|
||||
multiSet(value: V, ...keys: K) {
|
||||
const firstKey = keys[0];
|
||||
|
||||
Reference in New Issue
Block a user