-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpart_2.js
36 lines (28 loc) · 1.21 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
35
36
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 curly */
const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt")))
.trim()
.split("\n")
.flatMap(l => l.split(/\D+/).slice(-1).map(a => Number(a[0])));
const STORE = {};
const pStart = performance.now();
const solve = (p, cp, np, cs = 0, rs = 0, e = [p, cp, np, cs, rs].join("|"), wins = [0, 0]) => (STORE[e]) ? STORE[e] : (() => {
for (let i = 1; i <= 3; i++)
for (let j = 1; j <= 3; j++)
for (let k = 1; k <= 3; k++) (cs + ((cp + (i + j + k) - 1) % 10) + 1 >= 21) ? wins[p]++ : solve(
1 - p, np, ((cp + (i + j + k) - 1) % 10) + 1, rs,
cs + ((cp + (i + j + k) - 1) % 10) + 1).forEach((el, idx) => (wins[idx] += el),
);
return (STORE[e] = wins);
})();
const RES = Math.max(...solve(0, ...INPUT, 0, 0));
const pEnd = performance.now();
console.log("WINNING PLAYER'S UNIVERSE COUNT: " + RES);
console.log(pEnd - pStart);