Skip to content

Instantly share code, notes, and snippets.

@p-a
Created December 19, 2024 06:36
AoC 2024 Day 19
import { filter, length, sum } from "@/lib/arrays";
import { memo, pipe } from "@/lib/utils.js";
const parse = input =>
(([g1, g2]) => [
g1.split(",").map(v => v.trim()),
g2.split("\n"),
])(input.split("\n\n"));
export const part1 = pipe(
parse,
([patterns, designs]) => {
const isPossible = memo(
d =>
d === "" ||
patterns
.filter(p => d.startsWith(p))
.map(p => d.substring(p.length))
.filter(rest => isPossible(rest)).length > 0
);
return designs.map(d => isPossible(d));
},
filter(Boolean),
length
);
export const part2 = pipe(
parse,
([patterns, designs]) => {
const combinations = memo(d =>
d === ""
? 1
: patterns
.filter(p => d.startsWith(p))
.map(p => d.substring(p.length))
.reduce(
(sum, rest) => sum + combinations(rest),
0
)
);
return designs.map(d => combinations(d));
},
sum
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment