-
Notifications
You must be signed in to change notification settings - Fork 8
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
3 changed files
with
86 additions
and
0 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 @@ | ||
function next(stone) { | ||
if (stone === 0) { | ||
return [1]; | ||
} | ||
if (`${stone}`.length % 2 === 0) { | ||
const s = `${stone}`; | ||
return [ | ||
parseInt(s.slice(0, s.length / 2)), | ||
parseInt(s.slice(s.length / 2)), | ||
]; | ||
} | ||
return [stone * 2024]; | ||
} | ||
|
||
const memory = new Map(); | ||
function do25(stone) { | ||
if (memory.has(stone)) return memory.get(stone); | ||
let stones = [stone]; | ||
for (let j = 0; j < 25; j++) stones = stones.flatMap(next); | ||
memory.set(stone, stones); | ||
return stones; | ||
} | ||
|
||
// function doit(stone, times = 25) { | ||
// if (times === 0) return 1; | ||
|
||
// const key = `${stone},${times}`; | ||
// if (memory.has(key)) return memory.get(key); | ||
// let sum = 0; | ||
// let stones = next(stone); | ||
// stones.forEach(stone => { | ||
// sum += doit(stone, times - 1); | ||
// }); | ||
// memory.set(key, sum); | ||
// return sum; | ||
// } | ||
|
||
export function part1(input) { | ||
let stones = input.split(" ").map(Number); | ||
let sum = 0; | ||
stones.forEach(stone => { | ||
sum += do25(stone).length; | ||
}); | ||
return sum; | ||
} | ||
|
||
export function part2(input) { | ||
let stones = input.split(" ").map(Number); | ||
let sum = 0; | ||
stones.forEach(stone => { | ||
do25(stone).forEach(stone => { | ||
do25(stone).forEach(stone => { | ||
sum += do25(stone).length; | ||
}); | ||
}); | ||
}); | ||
return sum; | ||
} |
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 { part1, part2 } from "./day11.js"; | ||
import readInput from "../utils/read-input.js"; | ||
|
||
const input = readInput(import.meta.url); | ||
|
||
describe("day11 2024", () => { | ||
describe("part1", () => { | ||
test("it should work for part 1 examples", () => { | ||
expect(part1(["125 17"].join("\n"))).toEqual(55312); | ||
}); | ||
|
||
test("it should work for part 1 input", () => { | ||
expect(part1(input)).toEqual(197157); | ||
}); | ||
}); | ||
|
||
describe("part2", () => { | ||
test("it should work for part 2 examples", () => { | ||
// expect(part2(paste)).toEqual(paste); | ||
}); | ||
|
||
test("it should work for part 2 input", () => { | ||
expect(part2(input)).toEqual(234430066982597); | ||
}); | ||
}); | ||
}); |
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,2 @@ | ||
0 44 175060 3442 593 54398 9 8101095 | ||
|