-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24.ex
38 lines (31 loc) · 986 Bytes
/
day24.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
defmodule Y2015.Day24 do
use Advent.Day, no: 24
def part1(input), do: do_parts(input, 3)
def part2(input), do: do_parts(input, 4)
defp do_parts(input, num_buckets) do
bucket_size = div(Enum.sum(input), num_buckets)
check_bucket_size(input, bucket_size, 1)
end
def check_bucket_size(packages, bucket_size, count) do
matches =
Advent.combinations(packages, count)
|> Enum.filter(fn list -> Enum.sum(list) == bucket_size end)
if matches == [] do
check_bucket_size(packages, bucket_size, count + 1)
else
matches
|> Enum.min_by(&quantum_entanglement/1)
|> quantum_entanglement()
end
end
def quantum_entanglement(set) do
Enum.reduce(set, 1, fn x, acc -> acc * x end)
end
def parse_input(input) do
input
|> String.split("\n")
|> Enum.map(&String.to_integer/1)
end
def part1_verify, do: input() |> parse_input() |> part1()
def part2_verify, do: input() |> parse_input() |> part2()
end