diff --git a/README.md b/README.md index f12a5d7..03d85cd 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,8 @@ My Elixir solutions for [Advent of Code](https://adventofcode.com/) (all years). -
- +
diff --git a/lib/y2024/README.md b/lib/y2024/README.md
index 27fb3e4..b5436c6 100644
--- a/lib/y2024/README.md
+++ b/lib/y2024/README.md
@@ -2,7 +2,7 @@
My Elixir solutions for [Advent of Code 2024](https://adventofcode.com/2024).
-
+
## Benchmarks
@@ -48,4 +48,5 @@ day 16, part 2 13.07 76.51 ms ±5.00% 76.09 ms 85.
day 17, part 1 45.64 K 21.91 μs ±15.81% 21.17 μs 37.50 μs
day 18, part 1 22.53 44.38 ms ±5.11% 44.48 ms 48.54 ms
day 18, part 2 4.04 247.58 ms ±3.18% 247.68 ms 260.90 ms
+day 19, part 1 19.46 51.38 ms ±1.08% 51.16 ms 53.08 ms
```
diff --git a/lib/y2024/day19.ex b/lib/y2024/day19.ex
new file mode 100644
index 0000000..2d39c3b
--- /dev/null
+++ b/lib/y2024/day19.ex
@@ -0,0 +1,45 @@
+defmodule Y2024.Day19 do
+ use Advent.Day, no: 19
+
+ def part1(%{from: from_towels, to: to_towels}) do
+ Enum.count(to_towels, fn to ->
+ can_make?(to, from_towels)
+ end)
+ end
+
+ # @doc """
+ # iex> Day19.part2("update or delete me")
+ # "update or delete me"
+ # """
+ # def part2(input) do
+ # input
+ # end
+
+ defp can_make?(to, from_towels) do
+ find_towel_list([{to, []}], from_towels) != nil
+ end
+
+ defp find_towel_list([], _from_towels), do: nil
+ defp find_towel_list([{[], made} | _rest], _from_towels), do: Enum.reverse(made)
+
+ defp find_towel_list([{to, made} | rest], from_towels) do
+ new =
+ from_towels
+ |> Enum.filter(fn from -> List.starts_with?(to, from) end)
+ |> Enum.map(fn from -> {Enum.drop(to, length(from)), [from | made]} end)
+
+ find_towel_list(new ++ rest, from_towels)
+ end
+
+ def parse_input(input) do
+ [from, to] = String.split(input, "\n\n", trim: true)
+
+ %{
+ from: String.split(from, ", ") |> Enum.map(&String.graphemes/1),
+ to: String.split(to, "\n", trim: true) |> Enum.map(&String.graphemes/1)
+ }
+ end
+
+ def part1_verify, do: input() |> parse_input() |> part1()
+ # def part2_verify, do: input() |> parse_input() |> part2()
+end
diff --git a/lib/y2024/input/day19.txt b/lib/y2024/input/day19.txt
new file mode 100644
index 0000000..227db5e
Binary files /dev/null and b/lib/y2024/input/day19.txt differ
diff --git a/test/y2024/day19_test.exs b/test/y2024/day19_test.exs
new file mode 100644
index 0000000..3f74390
--- /dev/null
+++ b/test/y2024/day19_test.exs
@@ -0,0 +1,31 @@
+defmodule Y2024.Day19Test do
+ use ExUnit.Case, async: true
+ alias Y2024.Day19
+ doctest Day19
+
+ @sample """
+ r, wr, b, g, bwu, rb, gb, br
+
+ brwrr
+ bggr
+ gbbr
+ rrbgbr
+ ubwu
+ bwurrg
+ brgr
+ bbrgwb
+ """
+
+ test "part1" do
+ assert Day19.parse_input(@sample) |> Day19.part1() == 6
+ end
+
+ test "parse_input" do
+ assert %{from: from, to: to} = Day19.parse_input(@sample)
+ assert [["r"], ["w", "r"] | _rest] = from
+ assert [["b", "r", "w", "r", "r"], ["b", "g", "g", "r"] | _rest] = to
+ end
+
+ test "verification, part 1", do: assert(Day19.part1_verify() == 285)
+ # test "verification, part 2", do: assert(Day19.part2_verify() == "update or delete me")
+end