Skip to content

Commit

Permalink
19/2024
Browse files Browse the repository at this point in the history
  • Loading branch information
breadadams committed Dec 19, 2024
1 parent e2d2bec commit 3485e7e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 2024/19/example.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
25 changes: 25 additions & 0 deletions 2024/19/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect, describe, test } from "bun:test";
import { part1, part2 } from ".";
import { getInputs } from "../../utils/get-inputs";

const { exampleInput, puzzleInput } = await getInputs("2024/19");

describe("part 1", () => {
test("example", () => {
expect(part1(exampleInput)).toBe(6);
});

test("puzzle", () => {
expect(part1(puzzleInput)).toBe(358);
});
});

describe("part 2", () => {
test("example", () => {
expect(part2(exampleInput)).toBe(16);
});

test("puzzle", () => {
expect(part2(puzzleInput)).toBe(600639829400603);
});
});
78 changes: 78 additions & 0 deletions 2024/19/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { timePart1, timePart2 } from "../../utils/time-part";

const parseInput = (input: string) => {
const [availableTowels, designs] = input.split("\n\n");

return {
availableTowels: availableTowels.split(", "),
designs: designs.split("\n"),
};
};

export const part1 = timePart1((input: string) => {
const { availableTowels, designs } = parseInput(input);
let possibleDesigns = 0;

for (const design of designs) {
let queue: Array<{ remaining: string }> = [];

for (const towel of availableTowels) {
if (design.indexOf(towel) === 0) {
queue.push({ remaining: design.substring(towel.length) });
}
}

while (queue.length) {
queue.sort((a, b) => a.remaining.length - b.remaining.length);
const entry = queue.shift()!;

for (const towel of availableTowels) {
if (entry.remaining.indexOf(towel) === 0) {
if (entry.remaining.length === towel.length) {
possibleDesigns++;
queue = [];
break;
}

queue.push({ remaining: entry.remaining.substring(towel.length) });
}
}
}
}

return possibleDesigns;
});

export const part2 = timePart2((input: string) => {
const { availableTowels, designs } = parseInput(input);
let possibleDesigns = 0;
const canBeCompleted = new Map<string, number>();

const countTowelVariants = (remaining: string) => {
if (canBeCompleted.has(remaining)) {
return canBeCompleted.get(remaining)!;
}

let variants = 0;

for (const towel of availableTowels) {
if (remaining.indexOf(towel) === 0) {
if (remaining.length === towel.length) {
variants++;
continue;
}

variants += countTowelVariants(remaining.substring(towel.length));
}
}

canBeCompleted.set(remaining, variants);
return variants;
};

for (const design of designs) {
possibleDesigns += countTowelVariants(design);
}

return possibleDesigns;
});
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

| Day | Part 1 | Part 2 |
| :----------------------------------------: | :----: | :----: |
| [19](https://adventofcode.com/2024/day/19) |||
| [18](https://adventofcode.com/2024/day/18) |||
| [17](https://adventofcode.com/2024/day/17) |||
| [16](https://adventofcode.com/2024/day/16) |||
Expand Down

0 comments on commit 3485e7e

Please sign in to comment.