diff --git a/day11.peggy b/day11.peggy new file mode 100644 index 0000000..6f6630f --- /dev/null +++ b/day11.peggy @@ -0,0 +1,4 @@ +line = @num|.., _| "\n" + +num = n:$[0-9]+ { return parseInt(n, 10) } +_ = [ \t]+ diff --git a/day11.ts b/day11.ts new file mode 100644 index 0000000..ddfbb67 --- /dev/null +++ b/day11.ts @@ -0,0 +1,55 @@ +import { type MainArgs, parseFile } from './lib/utils.ts'; + +type Parsed = number[]; + +function mapAdd(m: Map, 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(); + for (const s of inp) { + mapAdd(prev, s); + } + for (let i = 0; i < num; i++) { + const next = new Map(); + 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(args); + return [part1(inp), part2(inp)]; +} diff --git a/inputs b/inputs index 0a75ccc..adcc810 160000 --- a/inputs +++ b/inputs @@ -1 +1 @@ -Subproject commit 0a75ccc7203878ef78b97ad56a70f1213fda7d7f +Subproject commit adcc810cac75bebe1e9194ea4028c46bb9694d1c diff --git a/test/day11.js b/test/day11.js new file mode 100644 index 0000000..88c9d93 --- /dev/null +++ b/test/day11.js @@ -0,0 +1 @@ +export default [182081, 216318908621637];