1
0
Fork 0

solve 2015 day 19 pt 2

This commit is contained in:
sloane 2024-11-26 14:25:14 -05:00
parent 5ec4f03f9d
commit 46f3ae87dc
Signed by: sloanelybutsurely
SSH key fingerprint: SHA256:8SBnwhl+RY3oEyQxy1a9wByPzxWM0x+/Ejc+sIlY5qQ
2 changed files with 25 additions and 1 deletions

View file

@ -49,5 +49,29 @@ aoc 2015, 19 do
end end
def p2 do 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
end end

View file

@ -6,7 +6,7 @@
| Year | Stars | Languages | | Year | Stars | Languages |
| - | - | - | | - | - | - |
| [2015] | **37/50** 🌟 | Elixir | | [2015] | **38/50** 🌟 | Elixir |
| 2016 | | | | 2016 | | |
| [2017] | **18/50** 🌟 | Haskell | | [2017] | **18/50** 🌟 | Haskell |
| 2018 | | | | 2018 | | |