Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Day 19

2024/day19.livemd

Day 19

Solution

{:ok, contents} = File.read("#{__DIR__}/inputs/day19.txt")
[raw_towels, raw_designs] = String.split(contents, "\n\n")
towels = String.split(raw_towels, ", ")
designs = raw_designs |> String.split("\n", trim: true)
defmodule Day19 do
  defp tail(string) do
    <<_::binary-1, rest::binary>> = string
    rest
  end

  def possible?(towels, design, i, dp) do
    ways = Map.get(dp, i, 0)

    cond do
      design == "" ->
        ways

      ways == 0 ->
        possible?(towels, tail(design), i + 1, dp)

      true ->
        dp_ =
          towels
          |> Enum.filter(&amp;String.starts_with?(design, &amp;1))
          |> Enum.map(&amp;String.length/1)
          |> Enum.reduce(dp, fn k, acc ->
            Map.update(acc, i + k, ways, &amp;(&amp;1 + ways))
          end)

        possible?(towels, tail(design), i + 1, dp_)
    end
  end
end

designs
|> Enum.map(&amp;Day19.possible?(towels, &amp;1, 0, %{0 => 1}))
|> Enum.sum()