-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.ex
55 lines (45 loc) · 1.29 KB
/
day06.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
48
49
50
51
52
53
54
55
defmodule Y2019.Day06 do
use Advent.Day, no: 6
def part1(input) do
input
|> parse_input
|> build_tree(:directed)
|> count_orbits
end
def part2(input) do
input
|> parse_input()
|> build_tree(:undirected)
|> length_of_path_between("YOU", "SAN")
end
defp build_tree(orbits, type) do
graph = :digraph.new()
Enum.reduce(orbits, graph, fn [object, orbiter], g ->
:digraph.add_vertex(g, object)
:digraph.add_vertex(g, orbiter)
:digraph.add_edge(g, object, orbiter)
if(type == :undirected) do
:digraph.add_edge(g, orbiter, object)
end
g
end)
end
defp length_of_path_between(graph, from, to) do
# number of transfers is number of hops between vertices, not including from and to
# path is just number of vertices
Enum.count(:digraph.get_short_path(graph, from, to)) - 3
end
defp count_orbits(graph) do
Enum.reduce(:digraph.vertices(graph), 0, fn vertex, count ->
count + Enum.count(:digraph_utils.reachable_neighbours([vertex], graph))
end)
end
defp parse_input(data) do
data
|> String.trim()
|> String.split("\n")
|> Enum.map(fn orbit -> String.split(orbit, ")") end)
end
def part1_verify, do: input() |> part1()
def part2_verify, do: input() |> part2()
end