solve 2015 day 19 pt 2
This commit is contained in:
parent
5ec4f03f9d
commit
46f3ae87dc
2 changed files with 25 additions and 1 deletions
|
@ -49,5 +49,29 @@ aoc 2015, 19 do
|
|||
end
|
||||
|
||||
def p2 do
|
||||
{mappings, molecule} = input()
|
||||
|
||||
find_shortest_path(molecule, invert(mappings))
|
||||
end
|
||||
|
||||
defp find_shortest_path(molecule, replacements, steps \\ 0)
|
||||
defp find_shortest_path("e", _replacements, steps), do: steps
|
||||
|
||||
defp find_shortest_path(molecule, replacements, steps) do
|
||||
{from, to} =
|
||||
Enum.find(replacements, fn {from, _} ->
|
||||
String.contains?(molecule, from)
|
||||
end)
|
||||
|
||||
molecule = String.replace(molecule, from, to, global: false)
|
||||
|
||||
find_shortest_path(molecule, replacements, steps + 1)
|
||||
end
|
||||
|
||||
defp invert(mappings) do
|
||||
for {from, tos} <- mappings, to <- tos do
|
||||
{to, from}
|
||||
end
|
||||
|> Enum.sort_by(fn {from, _} -> String.length(from) end, :desc)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
| Year | Stars | Languages |
|
||||
| - | - | - |
|
||||
| [2015] | **37/50** 🌟 | Elixir |
|
||||
| [2015] | **38/50** 🌟 | Elixir |
|
||||
| 2016 | | |
|
||||
| [2017] | **18/50** 🌟 | Haskell |
|
||||
| 2018 | | |
|
||||
|
|
Loading…
Reference in a new issue