Skip to content

Commit 2dcc667

Browse files
committed
feat: 2024 Day 19
1 parent 22ac990 commit 2dcc667

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

2024/day/19/part/1/solve.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import solve from "./solve.ts";
2+
3+
import { assertEquals } from "@std/assert";
4+
5+
Deno.test("example", () => {
6+
const input = `\
7+
r, wr, b, g, bwu, rb, gb, br
8+
9+
brwrr
10+
bggr
11+
gbbr
12+
rrbgbr
13+
ubwu
14+
bwurrg
15+
brgr
16+
bbrgwb`;
17+
18+
assertEquals(solve(input), 6);
19+
});

2024/day/19/part/1/solve.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default function solve(input: string) {
2+
const [top, bottom] = input.split("\n\n");
3+
const patterns = top.split(", ");
4+
const designs = bottom.split("\n");
5+
let possibleCount = 0;
6+
const tester = new RegExp(`^(?:${patterns.join("|")})+$`);
7+
for (const design of designs) if (tester.test(design)) possibleCount++;
8+
return possibleCount;
9+
}

2024/day/19/part/2/solve.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import solve from "./solve.ts";
2+
3+
import { assertEquals } from "@std/assert";
4+
5+
Deno.test("example", () => {
6+
const input = `\
7+
r, wr, b, g, bwu, rb, gb, br
8+
9+
brwrr
10+
bggr
11+
gbbr
12+
rrbgbr
13+
ubwu
14+
bwurrg
15+
brgr
16+
bbrgwb`;
17+
18+
assertEquals(solve(input), 16);
19+
});

2024/day/19/part/2/solve.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export default function solve(input: string) {
2+
const [top, bottom] = input.split("\n\n");
3+
const patterns = top.split(", ");
4+
const maxPatternLength = Math.max(...patterns.map(({ length }) => length));
5+
const designs = bottom.split("\n");
6+
let possibleCount = 0;
7+
const isPossibleRegExp = new RegExp(`^(?:${patterns.join("|")})+$`);
8+
for (const design of designs) {
9+
if (!isPossibleRegExp.test(design)) continue;
10+
const counts = Array.from({ length: design.length + 1 }, () => 0);
11+
counts[0] = 1;
12+
for (let j = 1; j <= design.length; j++) {
13+
for (let i = Math.max(0, j - maxPatternLength); i < j; i++) {
14+
if (patterns.includes(design.slice(i, j))) counts[j] += counts[i];
15+
}
16+
}
17+
possibleCount += counts.at(-1)!;
18+
}
19+
return possibleCount;
20+
}

0 commit comments

Comments
 (0)