-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.ex
87 lines (73 loc) · 1.86 KB
/
day08.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
defmodule Y2018.Day08 do
use Advent.Day, no: 8
@doc """
iex> Day08.part1("0 3 0 1 2")
3
iex> Day08.part1("1 3 0 1 55 0 1 2")
58
iex> Day08.part1("2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2")
138
"""
def part1(input) do
input
|> parse_input
|> sum_metadata
end
@doc """
iex> Day08.part2("2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2")
66
"""
def part2(input) do
input
|> parse_input
|> sum_value
end
defp sum_metadata(%{metadata: metadata, children: children}) do
Enum.sum(Enum.map(children, &sum_metadata/1)) + Enum.sum(metadata)
end
defp sum_value(%{children: [], metadata: metadata}) do
Enum.sum(metadata)
end
defp sum_value(%{children: children, metadata: metadata}) do
Enum.reduce(metadata, 0, fn x, acc ->
acc + get_child_metadata_value(x - 1, children)
end)
end
defp get_child_metadata_value(x, children) do
case Enum.at(children, x) do
nil -> 0
child -> sum_value(child)
end
end
def parse_input(input) do
input
|> String.trim()
|> String.split(" ")
|> Enum.map(&String.to_integer/1)
|> read_nodes([])
|> Enum.at(1)
|> hd
end
defp read_nodes([], acc), do: acc
defp read_nodes(input, acc) do
{node, rest} = read_node(input)
[rest, [node | acc]]
end
defp read_node([children_count, metadata_count | rest]) do
{rest, children} =
if children_count > 0 do
1..children_count
|> Enum.to_list()
|> Enum.reduce({rest, []}, fn _index, {new_rest, children} ->
{node, rest} = read_node(new_rest)
{rest, [node | children]}
end)
else
{rest, []}
end
{metadata, rest} = Enum.split(rest, metadata_count)
{%{children: Enum.reverse(children), metadata: metadata}, rest}
end
def part1_verify, do: input() |> part1()
def part2_verify, do: input() |> part2()
end