-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpart_2.js
34 lines (26 loc) · 1.08 KB
/
part_2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import fs from "node:fs";
import path from "node:path";
import { performance } from "node:perf_hooks";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// ========================= //
// = Copyright (c) NullDev = //
// ========================= //
/* eslint-disable no-nested-ternary */
const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt"))).trim().split(" ").map(Number);
const pStart = performance.now();
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("STONES AFTER BLINKING 75 TIMES: " + res);
console.log(pEnd - pStart);