Solve 2025-01

This commit is contained in:
2025-12-01 11:07:34 +01:00
parent 837a43b4c2
commit 0ed63a276e
4 changed files with 86 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="2025-01" type="BunRunConfiguration">
<option name="program" value="2025/01/run.ts" />
<option name="workingDirectory" value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
</component>
+22
View File
@@ -0,0 +1,22 @@
import { describe, expect, it } from 'bun:test';
import { solveFirst, solveSecond } from './index.ts';
describe('2025-01', () => {
const testInput = `L68
L30
R48
L5
R60
L55
L1
L99
R14
L82`;
it('first', () => {
expect(solveFirst(testInput)).toBe(3);
});
it('second', () => {
expect(solveSecond(testInput)).toBe(6);
expect(solveSecond(`R1000`)).toBe(10);
});
});
+45
View File
@@ -0,0 +1,45 @@
import { mod } from '../../utils/numbers.ts';
const size = 100;
const initialValue = 50;
function parseInput(input: string) {
const regex = /^([LR])(\d*)$/gm;
const match = input.matchAll(regex);
return Array.from(match, (m) => Number(m[2]) * (m[1] === 'L' ? -1 : 1));
}
export function solveFirst(input: string): number {
let zeros = 0;
parseInput(input).reduce((d, r) => {
const newDial = (d + r) % size;
if (newDial === 0) {
zeros++;
}
return newDial;
}, initialValue);
return zeros;
}
export function solveSecond(input: string): number {
const rots = parseInput(input);
let zeros = 0;
let dial = initialValue;
// brute force is easier than thinking
for (const rot of rots) {
if (rot === 0) {
continue;
}
for (let i = 0; i < Math.abs(rot); i++) {
dial = mod(dial + (rot > 0 ? 1 : -1), size);
if (dial === 0) {
zeros++;
}
}
}
return zeros;
}
+12
View File
@@ -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);