Solve 2025-05
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="2025-05" type="BunRunConfiguration">
|
||||||
|
<option name="program" value="2025/05/run.ts" />
|
||||||
|
<option name="workingDirectory" value="$PROJECT_DIR$" />
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import { describe, expect, it } from 'bun:test';
|
||||||
|
import { solveFirst, solveSecond } from './index.ts';
|
||||||
|
|
||||||
|
describe('2025-05', () => {
|
||||||
|
const testInput = `3-5
|
||||||
|
10-14
|
||||||
|
16-20
|
||||||
|
12-18
|
||||||
|
|
||||||
|
1
|
||||||
|
5
|
||||||
|
8
|
||||||
|
11
|
||||||
|
17
|
||||||
|
32`;
|
||||||
|
it('first', () => {
|
||||||
|
expect(solveFirst(testInput)).toBe(3);
|
||||||
|
});
|
||||||
|
it('second', () => {
|
||||||
|
expect(solveSecond(testInput)).toBe(14);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import type { Coordinate } from '../../utils/coordinates.ts';
|
||||||
|
|
||||||
|
function parseRanges(input: string) {
|
||||||
|
return Array.from(
|
||||||
|
input.matchAll(/(\d*)-(\d*)/gm),
|
||||||
|
(m) => [Number(m[1]), Number(m[2])] as Coordinate
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function solveFirst(input: string): number {
|
||||||
|
const [r, i] = input.split('\n\n');
|
||||||
|
|
||||||
|
const ranges = parseRanges(r);
|
||||||
|
|
||||||
|
const ingredients = i.split('\n').map(Number);
|
||||||
|
|
||||||
|
return ingredients.filter((i) =>
|
||||||
|
ranges.some(([from, to]) => i >= from && i <= to)
|
||||||
|
).length;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function solveSecond(input: string): number {
|
||||||
|
const ranges = parseRanges(input).toSorted((a, b) => a[0] - b[0]);
|
||||||
|
|
||||||
|
let total = 0;
|
||||||
|
|
||||||
|
ranges.reduce((lastEnd, curr) => {
|
||||||
|
if (curr[1] <= lastEnd) {
|
||||||
|
return lastEnd;
|
||||||
|
}
|
||||||
|
total += curr[1] - Math.max(lastEnd + 1, curr[0]) + 1;
|
||||||
|
return curr[1];
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
return total;
|
||||||
|
}
|
||||||
@@ -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