Skip to content

Commit 3de357d

Browse files
committed
perf(2024/day/19): leverage Set
from 55ms down to 9ms
1 parent 2dcc667 commit 3de357d

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ export default function solve(input: string) {
55
const designs = bottom.split("\n");
66
let possibleCount = 0;
77
const isPossibleRegExp = new RegExp(`^(?:${patterns.join("|")})+$`);
8+
const patternsSet = new Set(patterns);
89
for (const design of designs) {
910
if (!isPossibleRegExp.test(design)) continue;
1011
const counts = Array.from({ length: design.length + 1 }, () => 0);
1112
counts[0] = 1;
1213
for (let j = 1; j <= design.length; j++) {
1314
for (let i = Math.max(0, j - maxPatternLength); i < j; i++) {
14-
if (patterns.includes(design.slice(i, j))) counts[j] += counts[i];
15+
if (patternsSet.has(design.slice(i, j))) counts[j] += counts[i];
1516
}
1617
}
1718
possibleCount += counts.at(-1)!;

0 commit comments

Comments
 (0)