From 7bf97ef3fbb76a5f9ed23916b958839aed14b88f Mon Sep 17 00:00:00 2001 From: Sloane Perrault Date: Wed, 21 Sep 2022 09:19:53 -0400 Subject: [PATCH] solve 2015 day 15 --- 2015/README.md | 13 +++++++------ 2015/lib/2015/15.ex | 46 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/2015/README.md b/2015/README.md index d02479a..b6e8f1a 100644 --- a/2015/README.md +++ b/2015/README.md @@ -12,12 +12,12 @@ ``` -| S | M | T | W | T | F | S | -| :--: | :--: | :-: | :-: | :-: | :-: | :-: | -| | | [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 | | +| S | M | T | W | T | F | S | +| :--: | :--: | :-: | :-: | :-: | :-: | :-: | +| | | [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 | | [1]: ./lib/2015/1.ex [2]: ./lib/2015/2.ex @@ -33,3 +33,4 @@ [12]: ./lib/2015/12.ex [13]: ./lib/2015/13.ex [14]: ./lib/2015/14.ex +[15]: ./lib/2015/15.ex diff --git a/2015/lib/2015/15.ex b/2015/lib/2015/15.ex index 99ef125..af3f4ae 100644 --- a/2015/lib/2015/15.ex +++ b/2015/lib/2015/15.ex @@ -15,12 +15,52 @@ aoc 2015, 15 do {name, [capacity, durability, flavor, texture], calories} end + def compile_ingredient({_label, coefficients, _cal}) do + fn v -> coefficients |> Enum.map(&(&1 * v)) end + end + def p1 do - input_stream() - |> Stream.map(&parse_ingredient/1) - |> Enum.to_list() + ingredients = + input_stream() + |> 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 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