1
0
Fork 0

solve 2015 day 15

This commit is contained in:
Sloane Perrault 2022-09-21 09:19:53 -04:00
parent 1beb3e6341
commit 7bf97ef3fb
2 changed files with 50 additions and 9 deletions

View file

@ -16,7 +16,7 @@
| :--: | :--: | :-: | :-: | :-: | :-: | :-: | | :--: | :--: | :-: | :-: | :-: | :-: | :-: |
| | | [1] | [2] | [3] | [4] | [5] | | | | [1] | [2] | [3] | [4] | [5] |
| [6] | [7] | [8] | [9] | [10] | [11] | [12] | | [6] | [7] | [8] | [9] | [10] | [11] | [12] |
| [13] | [14] | 15 | 16 | 17 | 18 | 19 | | [13] | [14] | [15] | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | | | 20 | 21 | 22 | 23 | 24 | 25 | |
[1]: ./lib/2015/1.ex [1]: ./lib/2015/1.ex
@ -33,3 +33,4 @@
[12]: ./lib/2015/12.ex [12]: ./lib/2015/12.ex
[13]: ./lib/2015/13.ex [13]: ./lib/2015/13.ex
[14]: ./lib/2015/14.ex [14]: ./lib/2015/14.ex
[15]: ./lib/2015/15.ex

View file

@ -15,12 +15,52 @@ aoc 2015, 15 do
{name, [capacity, durability, flavor, texture], calories} {name, [capacity, durability, flavor, texture], calories}
end end
def compile_ingredient({_label, coefficients, _cal}) do
fn v -> coefficients |> Enum.map(&(&1 * v)) end
end
def p1 do def p1 do
ingredients =
input_stream() input_stream()
|> Stream.map(&parse_ingredient/1) |> Enum.map(&parse_ingredient/1)
|> Enum.to_list() |> Enum.map(&compile_ingredient/1)
for i <- 1..100,
j <- 1..(100 - i),
k <- 1..(100 - (i + j)),
l <- 1..(100 - (i + j + k)),
i + j + k + l == 100 do
Enum.zip(ingredients, [i, j, k, l])
|> Enum.map(fn {fun, arg} -> apply(fun, [arg]) end)
|> List.zip()
|> Enum.map(fn t -> max(0, Tuple.sum(t)) end)
|> Enum.product()
end
|> Enum.sort(:desc)
|> hd()
end end
def p2 do def p2 do
ingredients =
input_stream()
|> Enum.map(&parse_ingredient/1)
[m, n, o, p] = ingredients |> Enum.map(&elem(&1, 2))
compiled_ingredients = Enum.map(ingredients, &compile_ingredient/1)
for i <- 1..100,
j <- 1..(100 - i),
k <- 1..(100 - (i + j)),
l <- 1..(100 - (i + j + k)),
i + j + k + l == 100 and i * m + j * n + k * o + l * p == 500 do
Enum.zip(compiled_ingredients, [i, j, k, l])
|> Enum.map(fn {fun, arg} -> apply(fun, [arg]) end)
|> List.zip()
|> Enum.map(fn t -> max(0, Tuple.sum(t)) end)
|> Enum.product()
end
|> Enum.sort(:desc)
|> hd()
end end
end end