-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19b.js
36 lines (24 loc) · 778 Bytes
/
19b.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 {readInput} from "./lib.js";
const input = readInput(19);
const towels = input.at(0).split(', ');
const designs = input.slice(2);
const cache = new Map();
function isPossible(design, towels) {
if (design.length === 0) {
return 1;
}
if (cache.has(design)) {
return cache.get(design);
}
const possibleTowels = towels.filter(towel => design.includes(towel));
let count = 0;
for (const towel of possibleTowels) {
if (!design.startsWith(towel)) {
continue;
}
count += isPossible(design.slice(towel.length), possibleTowels);
}
cache.set(design, count);
return count;
}
console.log(designs.map((design) => isPossible(design, towels)).reduce((acc, count) => acc + count, 0));