-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.ts
106 lines (87 loc) · 2.97 KB
/
day12.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
import { assertEquals } from "https://deno.land/std@0.208.0/assert/mod.ts";
import { loadTestData, loadData } from "./utils.ts";
const part1test = 21;
const part2test = 525152;
const cache: { [key: string]: number } = {}
const numOptions = (springs: string, sizes: number[], inChunk: boolean, damagedAccum: number): number => {
const json = JSON.stringify({ springs, sizes, inChunk, damagedAccum });
if (cache[json]) {
return cache[json];
}
if (springs.length == 0) {
if (sizes.length == 0) {
cache[json] = 1;
return 1;
} else {
if (inChunk) {
if (sizes[0] == damagedAccum && sizes.length == 1) {
cache[json] = 1;
return 1;
}
}
cache[json] = 0;
return 0;
}
}
if (inChunk && springs[0] == '.') {
if (sizes[0] != damagedAccum) {
cache[json] = 0;
return 0;
} else {
cache[json] = numOptions(springs, sizes.slice(1), false, 0);
return cache[json];
}
}
if (springs[0] == '?') {
cache[json] = numOptions('#' + springs.substring(1), sizes, inChunk, damagedAccum) + numOptions('.' + springs.substring(1), sizes, inChunk, damagedAccum);
return cache[json];
}
if (springs[0] == '.') {
cache[json] = numOptions(springs.substring(1), sizes, inChunk, damagedAccum);
return cache[json];
}
if (springs[0] == '#') {
if (!inChunk) {
if (sizes.length == 0) {
cache[json] = 0;
return 0;
}
cache[json] = numOptions(springs, sizes, true, 0);
return cache[json];
} else {
cache[json] = numOptions(springs.substring(1), sizes, inChunk, damagedAccum + 1);
return cache[json];
}
}
throw ("What");
}
const part1 = (data: string) => {
const lines = data.split('\n');
let sum = 0;
for (const line of lines) {
const [springs, list] = line.split(" ");
const sizes = list.split(",").map(n => parseInt(n));
sum += numOptions(springs, sizes, false, 0);
}
return sum;
};
const part2 = (data: string) => {
const lines = data.split('\n');
let sum = 0;
let counter = 1;
for (const line of lines) {
const [fifthSprings, fifthList] = line.split(" ");
const springs = new Array(5).fill(fifthSprings).join("?");
const list = new Array(5).fill(fifthList).join(",");
const sizes = list.split(",").map(n => parseInt(n));
sum += numOptions(springs, sizes, false, 0);
console.log(counter++)
// smths wrong with the memoization lol
}
return sum;
};
assertEquals(part1(await loadTestData(12)), part1test)
console.log(part1(await loadData(12)))
// assertEquals(part2(await loadTestData(12)), part2test)
// console.log("Part 2 Tested")
// console.log(part2(await loadData(12)))