-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.exs
53 lines (42 loc) · 1.68 KB
/
day9.exs
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
defmodule Day9 do
def run(data) do
data |> to_charlist |> loop(0, 0, :normal, 0)
end
defp loop([], _group_depth, total_score, _state, garbage_count) do
{total_score, garbage_count}
end
# start next to cancel
defp loop([ ?! | rest], group_depth, total_score, :garbage, garbage_count) do
loop(rest, group_depth, total_score, :next_to_cancel, garbage_count)
end
# start next to cancel
defp loop([ _char | rest], group_depth, total_score, :next_to_cancel, garbage_count) do
loop(rest, group_depth, total_score, :garbage, garbage_count)
end
# start garbage
defp loop([ ?< | rest], group_depth, total_score, :normal, garbage_count) do
loop(rest, group_depth, total_score, :garbage, garbage_count)
end
# end garbage
defp loop([ ?> | rest], group_depth, total_score, :garbage, garbage_count) do
loop(rest, group_depth, total_score, :normal, garbage_count)
end
# start a group
defp loop([ ?{ | rest], group_depth, total_score, :normal, garbage_count) do
new_group_depth = group_depth + 1
loop(rest, new_group_depth, total_score + new_group_depth, :normal, garbage_count)
end
# end a group
defp loop([ ?} | rest], group_depth, total_score, :normal, garbage_count) do
loop(rest, group_depth - 1, total_score, :normal, garbage_count)
end
defp loop([_char | rest], group_depth, total_score, :normal, garbage_count) do
loop(rest, group_depth, total_score, :normal, garbage_count)
end
# a char inside garbage
defp loop([_char | rest], group_depth, total_score, :garbage, garbage_count) do
loop(rest, group_depth, total_score, :garbage, garbage_count + 1)
end
end
{:ok, data} = File.read("day9.txt")
IO.inspect Day9.run(data)