Skip to content

Commit

Permalink
day 19
Browse files Browse the repository at this point in the history
  • Loading branch information
merrywhether committed Dec 21, 2024
1 parent e20c152 commit a0aeb86
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/19/enput.txt

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions src/19/input.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { assertEquals } from "@std/assert";
import { main } from "./main.ts";

const target = "input";

Deno.test(`correct possible design count for ${target}`, async () => {
const result = await main(target);
assertEquals(result.possibleDesigns, 353);
});

Deno.test(`correct possible design combos for ${target}`, async () => {
const result = await main(target);
assertEquals(result.possibleDesignCombos, 880877787214477);
});
53 changes: 53 additions & 0 deletions src/19/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// { possibleDesigns: 353, possibleDesignCombos: 880877787214477 }
// Elapsed: 133ms

function testDesign(towels: string[], design: string): number {
const designCounter = new Array(design.length + 1).fill(0);
designCounter[0] = 1;
for (let i = 0; i <= design.length; i++) {
if (designCounter[i]) {
for (const towel of towels) {
if (
i + towel.length <= design.length &&
towel === design.slice(i, i + towel.length)
) {
designCounter[i + towel.length] += designCounter[i];
}
}
}
}

return designCounter[design.length];
}

export async function main(target = "input") {
const dirpath = new URL(".", import.meta.url).pathname;
const text = await Deno.readTextFile(`${dirpath}${target}.txt`);

const { patterns, designs } = text.split("\n").reduce((agg, line) => {
if (line.match(/^[wubrg]+$/)) {
agg.designs.push(line);
return agg;
}

const towelsMatch = line.match(/^[wubrg, ]+$/);
if (towelsMatch) {
agg.patterns = towelsMatch[0].split(", ");
}

return agg;
}, { patterns: [] as string[], designs: [] as string[] });

return designs.reduce((agg, design) => {
const combos = testDesign(patterns, design);
agg.possibleDesigns += combos > 0 ? 1 : 0;
agg.possibleDesignCombos += combos;
return agg;
}, { possibleDesigns: 0, possibleDesignCombos: 0 });
}

if (import.meta.main) {
const startTime = performance.now();
console.log(await main());
console.log(`Elapsed: ${Math.round(performance.now() - startTime)}ms`);
}
10 changes: 10 additions & 0 deletions src/19/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
r, wr, b, g, bwu, rb, gb, br

brwrr
bggr
gbbr
rrbgbr
ubwu
bwurrg
brgr
bbrgwb
14 changes: 14 additions & 0 deletions src/19/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { assertEquals } from "@std/assert";
import { main } from "./main.ts";

const target = "sample";

Deno.test(`correct possible design count for ${target}`, async () => {
const result = await main(target);
assertEquals(result.possibleDesigns, 6);
});

Deno.test(`correct possible design combos for ${target}`, async () => {
const result = await main(target);
assertEquals(result.possibleDesignCombos, 16);
});

0 comments on commit a0aeb86

Please sign in to comment.