-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
108 lines (94 loc) · 3.03 KB
/
index.ts
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
100
101
102
103
104
105
106
107
108
import run from "aocrunner";
const parseInput = (rawInput: string) => rawInput.split(" ").map(Number);
const part1 = (rawInput: string) => {
const input = parseInput(rawInput);
const cache = new Map<string, number>();
const nodeCount = (node: number, remainingIterations: number): number => {
const recurse = (newNode: number, newRemainingIterations: number): number => {
const cacheKey = `${newNode}:${newRemainingIterations}`
const cachedResult = cache.get(cacheKey);
if (cachedResult !== undefined) {
return cachedResult;
}
const newResult = nodeCount(newNode, newRemainingIterations);
cache.set(cacheKey, newResult);
return newResult;
}
if (remainingIterations === 0) {
return 1;
}
if (node === 0) {
return recurse(1, remainingIterations - 1);
} else if (Math.floor(Math.log10(node)) % 2 === 1) {
const expSize = Math.floor(Math.log10(node)+1)/2;
// console.log(`Splitting ${node} into ${node % Math.pow(10, expSize)} and ${Math.floor(node / Math.pow(10, expSize))}`)
return (
recurse(node % Math.pow(10, expSize), remainingIterations - 1) +
recurse(
Math.floor(node / Math.pow(10, expSize)),
remainingIterations - 1,
)
);
} else {
return recurse(node * 2024, remainingIterations - 1);
}
};
return input.map((node)=>nodeCount(node, 25)).reduce((a,b)=>a+b);
};
const part2 = (rawInput: string) => {
const input = parseInput(rawInput);
const cache = new Map<string, number>();
const nodeCount = (node: number, remainingIterations: number): number => {
const recurse = (newNode: number, newRemainingIterations: number): number => {
const cacheKey = `${newNode}:${newRemainingIterations}`
const cachedResult = cache.get(cacheKey);
if (cachedResult !== undefined) {
return cachedResult;
}
const newResult = nodeCount(newNode, newRemainingIterations);
cache.set(cacheKey, newResult);
return newResult;
}
if (remainingIterations === 0) {
return 1;
}
if (node === 0) {
return recurse(1, remainingIterations - 1);
} else if (Math.floor(Math.log10(node)) % 2 === 1) {
const expSize = Math.floor(Math.log10(node)+1)/2;
// console.log(`Splitting ${node} into ${node % Math.pow(10, expSize)} and ${Math.floor(node / Math.pow(10, expSize))}`)
return (
recurse(node % Math.pow(10, expSize), remainingIterations - 1) +
recurse(
Math.floor(node / Math.pow(10, expSize)),
remainingIterations - 1,
)
);
} else {
return recurse(node * 2024, remainingIterations - 1);
}
};
return input.map((node)=>nodeCount(node, 75)).reduce((a,b)=>a+b);
};
run({
part1: {
tests: [
{
input: `125 17`,
expected: 55312,
},
],
solution: part1,
},
part2: {
tests: [
// {
// input: ``,
// expected: "",
// },
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});