solve 2015 day 15
This commit is contained in:
parent
1beb3e6341
commit
7bf97ef3fb
2 changed files with 50 additions and 9 deletions
|
@ -12,12 +12,12 @@
|
||||||
```
|
```
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
| S | M | T | W | T | F | S |
|
| S | M | T | W | T | F | S |
|
||||||
| :--: | :--: | :-: | :-: | :-: | :-: | :-: |
|
| :--: | :--: | :-: | :-: | :-: | :-: | :-: |
|
||||||
| | | [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
|
||||||
[2]: ./lib/2015/2.ex
|
[2]: ./lib/2015/2.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
|
||||||
|
|
|
@ -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
|
||||||
input_stream()
|
ingredients =
|
||||||
|> Stream.map(&parse_ingredient/1)
|
input_stream()
|
||||||
|> Enum.to_list()
|
|> Enum.map(&parse_ingredient/1)
|
||||||
|
|> 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
|
||||||
|
|
Loading…
Reference in a new issue