Skip to content

Instantly share code, notes, and snippets.

@bluepichu
Created December 19, 2024 05:05
Show Gist options
  • Save bluepichu/ce352f75a0d928c0c38b2d3f623e3a04 to your computer and use it in GitHub Desktop.
Save bluepichu/ce352f75a0d928c0c38b2d3f623e3a04 to your computer and use it in GitHub Desktop.
import { Advent, f, fm, chr, ord, arr } from "advent";
import { Set, Map } from "immutable";
const { compute, computeCheck } = await Advent({ day: 19 });
compute(2, async (input) => {
const data = input.parse(f.dis("\n\n", f.split(", ", f.str()), f.nl(f.str()), (tow, targets) => ({ tow, targets })));
let ans = 0;
// const regex = new RegExp("^(" + data.tow.map((rule) => `(${rule})`).join("|") + ")+$");
// for (const target of data.targets) {
// if (regex.test(target)) {
// ans++;
// }
// }
for (const target of data.targets) {
const dp = arr(target.length + 1, () => 0);
dp[0] = 1;
for (let i = 0; i < target.length; i++) {
for (const tow of data.tow) {
if (target.slice(i).startsWith(tow)) {
dp[i + tow.length] += dp[i];
}
}
}
ans += dp[target.length];
}
return ans;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment