-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_12.ex
47 lines (40 loc) · 993 Bytes
/
day_12.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
defmodule Day12 do
@input File.read!("input/day_12.txt")
def run(:a) do
@input
|> parse
|> collect_programs("0", MapSet.new)
|> MapSet.size
end
def run(:b) do
pipes = @input |> parse
Map.keys(pipes)
|> Enum.map(fn(program) -> collect_programs(pipes, program, MapSet.new) end)
|> Enum.uniq
|> length
end
def collect_programs(pipes, program, group) do
if MapSet.member?(group, program) do
group
else
group = MapSet.put(group, program)
Map.fetch!(pipes, program)
|> Enum.reduce(group, fn(p, union) ->
MapSet.union(union, collect_programs(pipes, p, union))
end)
end
end
def parse(input) do
input
|> String.splitter("\n", trim: true)
|> Enum.map(&parse_line/1)
|> Enum.into(Map.new)
end
def parse_line(line) do
[from, to] = String.split(line, " <-> ")
to = String.split(to, ", ")
{from, to}
end
end
Day12.run(:a) |> IO.inspect
Day12.run(:b) |> IO.inspect