-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64a118c
commit 9218b5c
Showing
5 changed files
with
80 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |