-
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.
feat(js): add solutions for 2024 day 11
- Loading branch information
1 parent
64d014f
commit 7a0dc98
Showing
2 changed files
with
56 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,39 @@ | ||
import { solution, sum } from '../../utils/index.js'; | ||
|
||
const parse = (input) => input.trim().split(' ').map(Number); | ||
|
||
const blink = (stone, { times, cache = new Map() }) => { | ||
// A stone is just a single stone at one point | ||
if (times === 0) { | ||
return 1; | ||
} | ||
|
||
const hash = `${stone}x${times}`; | ||
if (cache.has(hash)) { | ||
return cache.get(hash); | ||
} | ||
|
||
const opts = { times: times - 1, cache }; | ||
|
||
const str = String(stone); | ||
let result; | ||
if (stone === 0) { | ||
result = blink(1, opts); | ||
} else if (str.length % 2 === 0) { | ||
result = blink(+str.slice(0, str.length / 2), opts) + blink(+str.slice(str.length / 2), opts); | ||
} else { | ||
result = blink(stone * 2024, opts); | ||
} | ||
cache.set(hash, result); | ||
return result; | ||
}; | ||
|
||
export const partOne = solution((input, { blinks: times = 25 }) => { | ||
const stones = parse(input); | ||
return sum(stones.map((stone) => blink(stone, { times }))); | ||
}); | ||
|
||
export const partTwo = solution((input, { blinks: times = 75 }) => { | ||
const stones = parse(input); | ||
return sum(stones.map((stone) => blink(stone, { times }))); | ||
}); |
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,17 @@ | ||
part-one: | ||
- input: | | ||
0 1 10 99 999 | ||
answer: 7 | ||
args: | ||
blinks: 1 | ||
- input: &example1 | | ||
125 17 | ||
answer: 22 | ||
args: | ||
blinks: 6 | ||
|
||
- input: *example1 | ||
answer: 55312 | ||
args: | ||
blinks: 25 |