Skip to content

Commit

Permalink
Day 11 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
NullDev committed Dec 11, 2024
1 parent da8f4e4 commit 6eff31e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
23 changes: 17 additions & 6 deletions 2024/Day_11/part_2.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,27 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
// = Copyright (c) NullDev = //
// ========================= //

const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt"))).trim().split("\n");
/* eslint-disable no-nested-ternary */

const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt"))).trim().split(" ").map(Number);

const pStart = performance.now();

//
// YOUR CODE HERE
//
const result = "...";
const mem = {};
const run = (
s, steps, key = `${s}|${steps}`,
) => ((steps === 0) ? 1 : ((key in mem)
? mem[key]
: (mem[key] = (s === 0)
? run(1, steps - 1)
: (String(s).length % 2 === 0)
? run(Number(String(s).substring(0, String(s).length / 2)), steps - 1)
+ run(Number(String(s).substring(String(s).length / 2)), steps - 1)
: run(s * 2024, steps - 1)), mem[key]));

const res = INPUT.map(s => run(s, 75)).reduce((acc, x) => acc + x);

const pEnd = performance.now();

console.log("<DESCRIPTION>: " + result);
console.log("STONES AFTER BLINKING 75 TIMES: " + res);
console.log(pEnd - pStart);
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ I also tried to experiment with a couple of different things:
- [DFS](https://en.wikipedia.org/wiki/Depth-first_search) with Memoization in [Day_07/part_2.js](https://github.com/NullDev/Advent-of-Code/blob/master/2024/Day_07/part_2.js)
- One-Liner in both [Day_08/part_1.js](https://github.com/NullDev/Advent-of-Code/blob/master/2024/Day_08/part_1.js) and [Day_08/part_2.js](https://github.com/NullDev/Advent-of-Code/blob/master/2024/Day_08/part_2.js)
- _Almost_ One-Liner [DFS](https://en.wikipedia.org/wiki/Depth-first_search) in both [Day_10/part_1.js](https://github.com/NullDev/Advent-of-Code/blob/master/2024/Day_10/part_1.js) and [Day_10/part_2.js](https://github.com/NullDev/Advent-of-Code/blob/master/2024/Day_10/part_2.js)
- Recursive memoized _almost_ One-Liner in [Day_11/part_1.js](https://github.com/NullDev/Advent-of-Code/blob/master/2024/Day_11/part_1.js) and [Day_11/part_2.js](https://github.com/NullDev/Advent-of-Code/blob/master/2024/Day_11/part_2.js)

<hr>

Expand Down

0 comments on commit 6eff31e

Please sign in to comment.