-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_11.ts
More file actions
99 lines (89 loc) · 2.48 KB
/
day_11.ts
File metadata and controls
99 lines (89 loc) · 2.48 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// const text = "125 17";
const text = await Deno.readTextFile("./day_11.txt");
// Part 1
let nums = text.split(" ").map(Number);
for (let i = 0; i < 25; i++) {
const newNums = [];
for (const num of nums) {
if (num === 0) {
newNums.push(1);
continue;
}
const str = `${num}`;
if (str.length % 2 === 0) {
const midPoint = str.length / 2;
const left = +str.slice(0, midPoint);
const right = +str.slice(midPoint);
newNums.push(left, right);
} else {
newNums.push(num * 2024);
}
}
nums = newNums;
// console.log(nums);
}
console.log(nums.length);
// Part 2
nums = text.split(" ").map(Number);
const cache = new Map<number, number[]>();
const cacheDepth = 5;
// let cacheDepth = 4;
function splitStonesIterative(num: number, depth = 25): number[] {
if (depth === cacheDepth && cache.has(num)) return cache.get(num)!;
let nums = [num];
for (let i = 0; i < depth; i++) {
const newNums = [];
for (const num of nums) {
if (num === 0) {
newNums.push(1);
continue;
}
const str = `${num}`;
if (str.length % 2 === 0) {
const midPoint = str.length / 2;
const left = +str.slice(0, midPoint);
const right = +str.slice(midPoint);
newNums.push(left, right);
} else {
newNums.push(num * 2024);
}
}
nums = newNums;
}
if (depth === cacheDepth) cache.set(num, nums);
return nums;
}
function recursiveIter(num: number, depth: number) {
const nums = splitStonesIterative(num, Math.min(cacheDepth, depth));
if (depth <= cacheDepth) return nums.length;
let total = 0;
for (const n of nums) {
total += recursiveIter(n, depth - cacheDepth);
}
return total;
}
// for (let i = 1; i < 30; i++) {
const start = performance.now();
let totalStones = 0;
for (const num of nums) {
// totalStones += splitStonesIterative(num, 25).length;
// totalStones += recursiveIter(num, i);
totalStones += recursiveIter(num, 50);
}
console.log(performance.now() - start);
// console.log(i, totalStones);
console.log(totalStones);
// }
// for (; cacheDepth < 25; cacheDepth++) {
// cache.clear();
// let correct = 0;
// Deno.bench(`cacheDepth: ${cacheDepth}`, () => {
// let totalStones = 0;
// for (const num of nums) {
// // totalStones += splitStonesIterative(num, 25).length;
// totalStones += recursiveIter(num, 25);
// }
// if (totalStones !== 55312) throw new Error("" + totalStones);
// correct++;
// });
// }