-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from hildjj/day11
Day 11
- Loading branch information
Showing
4 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)]; | ||
} |
Submodule inputs
updated
from 0a75cc to adcc81
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default [182081, 216318908621637]; |