Solve 2025-03
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="2025-03" type="BunRunConfiguration">
|
||||||
|
<option name="program" value="2025/03/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-03', () => {
|
||||||
|
const testInput = `987654321111111
|
||||||
|
811111111111119
|
||||||
|
234234234234278
|
||||||
|
818181911112111`;
|
||||||
|
it('first', () => {
|
||||||
|
expect(solveFirst(testInput)).toBe(357);
|
||||||
|
});
|
||||||
|
it('second', () => {
|
||||||
|
expect(solveSecond(testInput)).toBe(3121910778619);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
import { sumArray } from '../../utils/array.ts';
|
||||||
|
|
||||||
|
function findHighestDigit(digits: number[], start: number, end: number) {
|
||||||
|
let highest = 0;
|
||||||
|
let highestI = start;
|
||||||
|
|
||||||
|
for (let i = start; i < digits.length - end; i++) {
|
||||||
|
if (digits[i] > highest) {
|
||||||
|
highest = digits[i];
|
||||||
|
highestI = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return highestI;
|
||||||
|
}
|
||||||
|
|
||||||
|
function findHighestNumber(row: string, size: number) {
|
||||||
|
const rowSplit = row.split('');
|
||||||
|
const digits = rowSplit.map(Number);
|
||||||
|
|
||||||
|
const highest: number[] = [];
|
||||||
|
for (let i = 0; i < size; i++) {
|
||||||
|
const highestDigit = findHighestDigit(
|
||||||
|
digits,
|
||||||
|
(i > 0 ? highest[i - 1] : -1) + 1,
|
||||||
|
size - i - 1
|
||||||
|
);
|
||||||
|
|
||||||
|
highest.push(highestDigit);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Number(highest.map((key) => rowSplit[key]).join(''));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function solveFirst(input: string): number {
|
||||||
|
const rows = input.split('\n');
|
||||||
|
|
||||||
|
return sumArray(rows.map((row) => findHighestNumber(row, 2)));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function solveSecond(input: string): number {
|
||||||
|
const rows = input.split('\n');
|
||||||
|
|
||||||
|
return sumArray(rows.map((row) => findHighestNumber(row, 12)));
|
||||||
|
}
|
||||||
@@ -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