-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_09.ex
48 lines (43 loc) · 1.17 KB
/
day_09.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
defmodule Day09 do
@input File.read!("input/day_09.txt")
defmodule Result do
defstruct log: [], depth: 0, count: 0
end
def run do
@input
|> String.trim_trailing
|> parse
end
def parse(input), do: parse(input, %Result{})
def parse("", result) do
groups_score = result.log |> Enum.sum
garbage_score = result.count
{groups_score, garbage_score}
end
def parse("{" <> rest, score) do
parse(rest, %{score | depth: score.depth + 1})
end
def parse("}" <> rest, score) do
parse(rest, %{score | depth: score.depth - 1, log: [score.depth | score.log]})
end
def parse("," <> rest, score) do
parse(rest, score)
end
def parse("!" <> <<_::bytes-size(1)>> <> rest, score) do
parse(rest, score)
end
def parse("<" <> rest, score) do
{rest, score} = count_garbage(rest, score)
parse(rest, score)
end
def count_garbage(">" <> rest, score) do
{rest, score}
end
def count_garbage("!" <> <<_::bytes-size(1)>> <> rest, score) do
count_garbage(rest, score)
end
def count_garbage(<<_::bytes-size(1)>> <> rest, score) do
count_garbage(rest, %{score | count: score.count + 1})
end
end
Day09.run |> IO.inspect