46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
|
|
const fs = require('fs');
|
||
|
|
|
||
|
|
const input = fs.readFileSync(__dirname + '/input.txt', 'utf8').split("\n");
|
||
|
|
|
||
|
|
const towels: Array<string> = input[0].split(', ');
|
||
|
|
|
||
|
|
const shortestTowel = Math.min(...towels.map((t: string) => t.length));
|
||
|
|
const longestTowel = Math.max(...towels.map((t: string) => t.length));
|
||
|
|
|
||
|
|
const designs = input.slice(2);
|
||
|
|
|
||
|
|
let possible = 0;
|
||
|
|
|
||
|
|
let permSum = 0;
|
||
|
|
|
||
|
|
let patternMap: { [key: string]: number } = {};
|
||
|
|
|
||
|
|
function canBeDone(pattern: string): number {
|
||
|
|
if (Object.hasOwn(patternMap, pattern)) {
|
||
|
|
return patternMap[pattern];
|
||
|
|
}
|
||
|
|
let perms = 0;
|
||
|
|
towels.forEach((t) => {
|
||
|
|
if (t === pattern) {
|
||
|
|
patternMap[pattern] = 1;
|
||
|
|
perms++;
|
||
|
|
} else if (pattern.startsWith(t)) {
|
||
|
|
const subPerms = canBeDone(pattern.substring(t.length));
|
||
|
|
perms += subPerms;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
patternMap[pattern] = perms;
|
||
|
|
return perms;
|
||
|
|
}
|
||
|
|
|
||
|
|
designs.forEach((d: string, i: number)=> {
|
||
|
|
patternMap = {};
|
||
|
|
console.log(Math.floor(i / designs.length * 100));
|
||
|
|
const designPerms = canBeDone(d);
|
||
|
|
if (designPerms) {
|
||
|
|
possible++;
|
||
|
|
permSum += designPerms;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log(possible, permSum);
|