Solve 2025-06
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="2025-06" type="BunRunConfiguration">
|
||||||
|
<option name="program" value="2025/06/run.ts" />
|
||||||
|
<option name="workingDirectory" value="$PROJECT_DIR$" />
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import { describe, expect, it } from 'bun:test';
|
||||||
|
import { solveFirst, solveSecond } from './index.ts';
|
||||||
|
|
||||||
|
describe('2025-06', () => {
|
||||||
|
const testInput = `123 328 51 64
|
||||||
|
45 64 387 23
|
||||||
|
6 98 215 314
|
||||||
|
* + * + `;
|
||||||
|
it('first', () => {
|
||||||
|
expect(solveFirst(testInput)).toBe(4277556);
|
||||||
|
});
|
||||||
|
it('second', () => {
|
||||||
|
expect(solveSecond(testInput)).toBe(3263827);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import { multArray, sumArray } from '../../utils/array.ts';
|
||||||
|
|
||||||
|
function sumOrMultArray(op: string, nums: number[]) {
|
||||||
|
if (op === '*') {
|
||||||
|
return multArray(nums);
|
||||||
|
}
|
||||||
|
return sumArray(nums);
|
||||||
|
}
|
||||||
|
export function solveFirst(input: string): number {
|
||||||
|
const rows = input.split('\n');
|
||||||
|
|
||||||
|
const ops = rows.splice(-1)[0].replaceAll(' ', '').split('');
|
||||||
|
const parsed = rows.map((r) =>
|
||||||
|
Array.from(r.matchAll(/(\d+)/g), (m) => m.slice(1).map(Number)).flat()
|
||||||
|
);
|
||||||
|
|
||||||
|
return ops.reduce(
|
||||||
|
(s, op, i) =>
|
||||||
|
s +
|
||||||
|
sumOrMultArray(
|
||||||
|
op,
|
||||||
|
parsed.map((p) => p[i])
|
||||||
|
),
|
||||||
|
0
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function solveSecond(input: string): number {
|
||||||
|
const rows = input.split('\n');
|
||||||
|
const longestRow = Math.max(...rows.map((r) => r.length));
|
||||||
|
|
||||||
|
const ops = rows.splice(-1)[0];
|
||||||
|
|
||||||
|
const cache: number[] = [];
|
||||||
|
let lastOp = '';
|
||||||
|
let sum = 0;
|
||||||
|
|
||||||
|
for (let i = 0; i < longestRow; i++) {
|
||||||
|
if (ops[i] === '*' || ops[i] === '+') {
|
||||||
|
sum += sumOrMultArray(lastOp, cache);
|
||||||
|
cache.length = 0;
|
||||||
|
lastOp = ops[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
const digits = parseInt(rows.map((r) => r[i]).join(''));
|
||||||
|
if (!isNaN(digits)) {
|
||||||
|
cache.push(Number(digits));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sum += sumOrMultArray(lastOp, cache);
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
@@ -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