AoC20/lib/day10/day10.ex

114 lines
1.7 KiB
Elixir

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