This commit is contained in:
Shadowfacts 2020-12-10 14:35:56 -05:00
parent ce94696d30
commit fa3b2ea34d
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 205 additions and 0 deletions

113
lib/day10/day10.ex Normal file
View File

@ -0,0 +1,113 @@
defmodule Day10 do
@example1 """
16
10
15
5
1
11
7
19
6
12
4
"""
@example2 """
28
33
18
42
31
14
46
20
48
47
24
23
49
45
19
38
39
11
1
32
25
35
8
17
7
9
4
2
34
10
3
"""
def part1(example \\ false) do
inputs = parse_input(example)
differences = get_differences(inputs)
Enum.count(differences, &(&1 == 1)) * Enum.count(differences, &(&1 == 3))
end
def part2(example \\ false) do
inputs = parse_input(example)
differences = get_differences(inputs)
count_sequences_of_ones(differences, [0])
|> Enum.map(fn x -> count_paths(x, 3) end)
|> Enum.reduce(1, &(&1 * &2))
end
def parse_input(example) do
case example do
false -> File.read!("lib/day10/input.txt")
1 -> @example1
2 -> @example2
end
|> String.trim()
|> String.split("\n")
|> Enum.map(&String.to_integer/1)
|> Enum.sort()
end
def get_differences(inputs) do
[0 | inputs]
|> Enum.zip(inputs ++ [List.last(inputs) + 3])
|> Enum.map(fn {a, b} -> b - a end)
end
def count_sequences_of_ones([cur_diff | rest_diffs], [cur_count | rest]) do
case cur_diff do
1 ->
count_sequences_of_ones(rest_diffs, [cur_count + 1 | rest])
_ ->
count_sequences_of_ones(rest_diffs, [0, cur_count | rest])
end
end
def count_sequences_of_ones([], counts) do
counts
end
def count_paths(length, _max_diff) when length <= 1 do
1
end
def count_paths(length, max_diff) do
IO.puts("count_paths(#{length}, #{max_diff})")
1..min(length, max_diff)
|> Enum.map(fn x ->
count_paths(length - x, max_diff)
end)
|> Enum.sum()
end
end

92
lib/day10/input.txt Normal file
View File

@ -0,0 +1,92 @@
56
139
42
28
3
87
142
57
147
6
117
95
2
112
107
54
146
104
40
26
136
127
111
47
8
24
13
92
18
130
141
37
81
148
31
62
50
80
91
33
77
1
96
100
9
120
27
97
60
102
25
83
55
118
19
113
49
133
14
119
88
124
110
145
65
21
7
74
72
61
103
20
41
53
32
44
10
34
121
114
67
69
66
82
101
68
84
48
73
17
43
140