Day 19 and 20

This commit is contained in:
2024-12-20 13:26:37 +00:00
parent 64d12de0f4
commit 471c797821
2 changed files with 153 additions and 0 deletions

46
2024/19/index.ts Normal file
View File

@@ -0,0 +1,46 @@
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);