Skip to content

Commit

Permalink
day 11 (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
spalberg authored Dec 14, 2024
1 parent 6cadd75 commit 275211b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
58 changes: 58 additions & 0 deletions days/11/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { loadInput } from "utils";

export function part1(input: Array<string>) {
const stones = parseInput(input);
const blink25 = blink(25);
return stones.map(blink25).reduce((a, b) => a + b);
}

export function part2(input: Array<string>) {
const stones = parseInput(input);
const blink75 = blink(75);
return stones.map(blink75).reduce((a, b) => a + b);
}

function blink(times: number) {
const cache = new Map<string, number>();
return (stone: number) => blinkRec(stone, times, cache);
}

function blinkRec(
stone: number,
times: number,
cache: Map<string, number>,
): number {
if (times === 0) {
return 1;
}
const cacheHit = cache.get(`${stone}-${times}`);
if (cacheHit != null) {
return cacheHit;
}
if (stone === 0) {
const result = blinkRec(1, times - 1, cache);
cache.set(`${stone}-${times}`, result);
return result;
}
const str = stone.toString();
if (str.length % 2 === 0) {
const half = str.length / 2;
const left = blinkRec(parseInt(str.slice(0, half), 10), times - 1, cache);
const right = blinkRec(parseInt(str.slice(half), 10), times - 1, cache);
const result = left + right;
cache.set(`${stone}-${times}`, result);
return result;
}
const result = blinkRec(stone * 2024, times - 1, cache);
cache.set(`${stone}-${times}`, result);
return result;
}

function parseInput(input: Array<string>): Array<number> {
return input[0].split(" ").map(Number);
}

if (import.meta.main) {
console.log(part1(await loadInput(11)));
console.log(part2(await loadInput(11)));
}
26 changes: 26 additions & 0 deletions days/11/main_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect } from "@std/expect";
import { describe, it } from "@std/testing/bdd";
import { part1, part2 } from "./main.ts";
import { loadInput } from "utils";

describe("day 11 example", () => {
const input = ["125 17"];

it("part 1", () => {
expect(part1(input)).toBe(55312);
});

it("part 2", () => {
expect(part2(input)).toBe(65601038650482);
});
});

describe("day 11 solution", () => {
it("part 1", async () => {
expect(part1(await loadInput(11))).toBe(239714);
});

it("part 2", async () => {
expect(part2(await loadInput(11))).toBe(284973560658514);
});
});
2 changes: 1 addition & 1 deletion days/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ type Solution = { part1: PartFunction; part2: PartFunction };

const days = new Map<number, Solution>();

for (const day of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) {
for (const day of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) {
days.set(day, await import(`./${day}/main.ts`));
}

Expand Down
2 changes: 1 addition & 1 deletion inputs

0 comments on commit 275211b

Please sign in to comment.