Skip to content

Commit

Permalink
Merge pull request #23 from hildjj/day11
Browse files Browse the repository at this point in the history
Day 11
  • Loading branch information
hildjj authored Dec 11, 2024
2 parents e561d59 + c2aa0df commit 4c7123e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
4 changes: 4 additions & 0 deletions day11.peggy
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
line = @num|.., _| "\n"

num = n:$[0-9]+ { return parseInt(n, 10) }
_ = [ \t]+
55 changes: 55 additions & 0 deletions day11.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { type MainArgs, parseFile } from './lib/utils.ts';

type Parsed = number[];

function mapAdd(m: Map<number, number>, k: number, v = 1): void {
const se = m.get(k);
m.set(k, (se ?? 0) + v);
}

function blink(inp: Parsed, num: number): number {
// Each stone k occurs v times. On a new iteration, there are a multiple
// ways that a given numbered stone might be created. Add all of those ways.
// However, no matter how a stone got created, it will have the same set of
// descendants.
let prev = new Map<number, number>();
for (const s of inp) {
mapAdd(prev, s);
}
for (let i = 0; i < num; i++) {
const next = new Map<number, number>();
for (const [k, v] of prev) {
if (k === 0) {
mapAdd(next, 1, v);
} else {
const ks = String(k);
if (ks.length % 2 === 0) {
const kl = ks.length / 2;
mapAdd(next, Number(ks.slice(0, kl)), v);
mapAdd(next, Number(ks.slice(kl)), v);
} else {
mapAdd(next, k * 2024, v);
}
}
}
prev = next;
}
let count = 0;
for (const [_k, v] of prev) {
count += v;
}
return count;
}

function part1(inp: Parsed): number {
return blink(inp, 25);
}

function part2(inp: Parsed): number {
return blink(inp, 75);
}

export default async function main(args: MainArgs): Promise<[number, number]> {
const inp = await parseFile<Parsed>(args);
return [part1(inp), part2(inp)];
}
2 changes: 1 addition & 1 deletion inputs
1 change: 1 addition & 0 deletions test/day11.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default [182081, 216318908621637];

0 comments on commit 4c7123e

Please sign in to comment.