diff --git a/2025/01/2025-01.run.xml b/2025/01/2025-01.run.xml new file mode 100644 index 0000000..2c383da --- /dev/null +++ b/2025/01/2025-01.run.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/2025/01/index.test.ts b/2025/01/index.test.ts new file mode 100644 index 0000000..6999a82 --- /dev/null +++ b/2025/01/index.test.ts @@ -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); + }); +}); diff --git a/2025/01/index.ts b/2025/01/index.ts new file mode 100644 index 0000000..67e7996 --- /dev/null +++ b/2025/01/index.ts @@ -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; +} diff --git a/2025/01/run.ts b/2025/01/run.ts new file mode 100644 index 0000000..88783cd --- /dev/null +++ b/2025/01/run.ts @@ -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);