Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule Day12 do
- def part1(), do:
- connections() |> group("0") |> Enum.count()
- def part2(), do:
- connections() |> all_groups() |> Enum.count()
- defp connections(), do:
- "input.txt"
- |> File.stream!()
- |> Stream.map(&String.trim/1)
- |> Stream.map(&String.split(&1, " <-> "))
- |> Stream.map(fn [element, neighbours] -> {element, String.split(neighbours, ", ")} end)
- |> Map.new()
- defp all_groups(connections), do:
- Enum.reduce(
- Map.keys(connections),
- [],
- fn program, groups ->
- if Enum.any?(groups, &MapSet.member?(&1, program)),
- do: groups,
- else: [group(connections, program) | groups]
- end
- )
- defp group(connections, program) do
- {neighbours, other_connections} = Map.pop(connections, program, MapSet.new())
- Enum.reduce(neighbours, MapSet.new([program]), &MapSet.union(&2, group(other_connections, &1)))
- end
- end
- Day12.part1() |> IO.inspect
- Day12.part2() |> IO.inspect
Add Comment
Please, Sign In to add comment