Advent of Code 2024 - Day 19
Mix.install([
{:kino_aoc, "~> 0.1.7"}
])
Section
{:ok, puzzle_input} =
KinoAOC.download_puzzle("2024", "19", System.fetch_env!("LB_AOC_SESSION"))
[towels, _ | designs] = String.split(puzzle_input, "\n")
towels = String.split(towels, ", ")
defmodule AoC2024.Day19 do
def run(towels, patterns) do
Task.async(fn ->
Enum.map(patterns, &combo(towels, towels, &1))
end)
|> Task.await(:infinity)
end
defp combo(_, _, "") do
1
end
defp combo([], _, _pattern) do
0
end
defp combo([towel | rest_towels], all_towels, pattern) do
memoized(pattern, fn ->
case pattern do
^towel <> rest_pattern ->
combo1 = combo(all_towels, all_towels, rest_pattern)
combo2 = combo(rest_towels, all_towels, pattern)
combo1 + combo2
_ ->
combo(rest_towels, all_towels, pattern)
end
end)
end
defp memoized(key, fun) do
with :miss <- Process.get(key, :miss) do
fun.() |> tap(&Process.put(key, &1))
end
end
end
Part 1
AoC2024.Day19.run(towels, designs)
|> Enum.count(& &1 > 0)
Part 2
AoC2024.Day19.run(towels, designs)
|> Enum.sum()