From 6eff31e69c772ad87bae930bc5161da2f597ae66 Mon Sep 17 00:00:00 2001 From: "NullDev (Shadow)" Date: Wed, 11 Dec 2024 06:39:01 +0100 Subject: [PATCH] Day 11 part 2 --- 2024/Day_11/part_2.js | 23 +++++++++++++++++------ README.md | 1 + 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/2024/Day_11/part_2.js b/2024/Day_11/part_2.js index 6376d62..2150883 100644 --- a/2024/Day_11/part_2.js +++ b/2024/Day_11/part_2.js @@ -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(": " + result); +console.log("STONES AFTER BLINKING 75 TIMES: " + res); console.log(pEnd - pStart); diff --git a/README.md b/README.md index 902e9b7..f2e8e24 100644 --- a/README.md +++ b/README.md @@ -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)