I managed to finally solve part 2 of the puzzle in Elixir, but not in a particular elegant way:
Inspired by Liam Mitchell’s elegant solution in the Elixir forum, I implemented this Erlang solution: