-
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.
- Loading branch information
Showing
4 changed files
with
86 additions
and
2 deletions.
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,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))); | ||
} |
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,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); | ||
}); | ||
}); |
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
Submodule inputs
updated
from 22f292 to 94fe76