Day 19 in TS again b/c why not and simple

This commit is contained in:
Tyler Beckman 2024-12-18 23:17:56 -07:00
parent d3520308c2
commit b42f579fbb
Signed by: Ty
GPG key ID: 2813440C772555A4
2 changed files with 54 additions and 0 deletions

26
day19/part1.ts Normal file
View file

@ -0,0 +1,26 @@
function attempt(towels: string[], pattern: string): boolean {
if (pattern == "") return true;
let anySucceeded = false;
for (const towel of towels) {
if (pattern.startsWith(towel)) {
anySucceeded = anySucceeded || attempt(towels, pattern.substring(towel.length));
}
}
return anySucceeded;
}
export async function solve() {
const [towels, patterns] = await Deno.readTextFile(
import.meta.dirname + "/input.txt",
).then(
(c) => [c.split("\n\n")[0].trim().split(", "), c.split("\n\n")[1].trim().split("\n")]
);
towels.sort((a, b) => b.length - a.length);
let sum = 0;
for (const pattern of patterns) {
if (attempt(towels, pattern)) sum++;
}
return sum
}

28
day19/part2.ts Normal file
View file

@ -0,0 +1,28 @@
const cache: Record<string, number> = {};
function attempt(towels: string[], pattern: string): number {
if (pattern.length <= 0) return 1;
if (pattern in cache) return cache[pattern];
const val = towels
.filter(t => pattern.startsWith(t))
.map(t => attempt(towels, pattern.substring(t.length)))
.reduce((acc, cur) => acc + cur, 0);
cache[pattern] = val;
return val;
}
export async function solve() {
const [towels, patterns] = await Deno.readTextFile(
import.meta.dirname + "/input.txt",
).then(
(c) => [c.split("\n\n")[0].trim().split(", "), c.split("\n\n")[1].trim().split("\n")]
);
let sum = 0;
for (const pattern of patterns) {
sum += attempt(towels, pattern);
}
return sum
}