Skip to content

Commit 3485e7e

Browse files
committed
19/2024
1 parent e2d2bec commit 3485e7e

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

2024/19/example.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
r, wr, b, g, bwu, rb, gb, br
2+
3+
brwrr
4+
bggr
5+
gbbr
6+
rrbgbr
7+
ubwu
8+
bwurrg
9+
brgr
10+
bbrgwb

2024/19/index.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { expect, describe, test } from "bun:test";
2+
import { part1, part2 } from ".";
3+
import { getInputs } from "../../utils/get-inputs";
4+
5+
const { exampleInput, puzzleInput } = await getInputs("2024/19");
6+
7+
describe("part 1", () => {
8+
test("example", () => {
9+
expect(part1(exampleInput)).toBe(6);
10+
});
11+
12+
test("puzzle", () => {
13+
expect(part1(puzzleInput)).toBe(358);
14+
});
15+
});
16+
17+
describe("part 2", () => {
18+
test("example", () => {
19+
expect(part2(exampleInput)).toBe(16);
20+
});
21+
22+
test("puzzle", () => {
23+
expect(part2(puzzleInput)).toBe(600639829400603);
24+
});
25+
});

2024/19/index.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { timePart1, timePart2 } from "../../utils/time-part";
2+
3+
const parseInput = (input: string) => {
4+
const [availableTowels, designs] = input.split("\n\n");
5+
6+
return {
7+
availableTowels: availableTowels.split(", "),
8+
designs: designs.split("\n"),
9+
};
10+
};
11+
12+
export const part1 = timePart1((input: string) => {
13+
const { availableTowels, designs } = parseInput(input);
14+
let possibleDesigns = 0;
15+
16+
for (const design of designs) {
17+
let queue: Array<{ remaining: string }> = [];
18+
19+
for (const towel of availableTowels) {
20+
if (design.indexOf(towel) === 0) {
21+
queue.push({ remaining: design.substring(towel.length) });
22+
}
23+
}
24+
25+
while (queue.length) {
26+
queue.sort((a, b) => a.remaining.length - b.remaining.length);
27+
const entry = queue.shift()!;
28+
29+
for (const towel of availableTowels) {
30+
if (entry.remaining.indexOf(towel) === 0) {
31+
if (entry.remaining.length === towel.length) {
32+
possibleDesigns++;
33+
queue = [];
34+
break;
35+
}
36+
37+
queue.push({ remaining: entry.remaining.substring(towel.length) });
38+
}
39+
}
40+
}
41+
}
42+
43+
return possibleDesigns;
44+
});
45+
46+
export const part2 = timePart2((input: string) => {
47+
const { availableTowels, designs } = parseInput(input);
48+
let possibleDesigns = 0;
49+
const canBeCompleted = new Map<string, number>();
50+
51+
const countTowelVariants = (remaining: string) => {
52+
if (canBeCompleted.has(remaining)) {
53+
return canBeCompleted.get(remaining)!;
54+
}
55+
56+
let variants = 0;
57+
58+
for (const towel of availableTowels) {
59+
if (remaining.indexOf(towel) === 0) {
60+
if (remaining.length === towel.length) {
61+
variants++;
62+
continue;
63+
}
64+
65+
variants += countTowelVariants(remaining.substring(towel.length));
66+
}
67+
}
68+
69+
canBeCompleted.set(remaining, variants);
70+
return variants;
71+
};
72+
73+
for (const design of designs) {
74+
possibleDesigns += countTowelVariants(design);
75+
}
76+
77+
return possibleDesigns;
78+
});

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
| Day | Part 1 | Part 2 |
88
| :----------------------------------------: | :----: | :----: |
9+
| [19](https://adventofcode.com/2024/day/19) |||
910
| [18](https://adventofcode.com/2024/day/18) |||
1011
| [17](https://adventofcode.com/2024/day/17) |||
1112
| [16](https://adventofcode.com/2024/day/16) |||

0 commit comments

Comments
 (0)