/aoc-2024-day-19.js Secret
Created
December 19, 2024 06:36
AoC 2024 Day 19
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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