From 699496aa0bbb5d0168cc931c99e7c4717be8020c Mon Sep 17 00:00:00 2001 From: Sloane Perrault Date: Wed, 21 Sep 2022 09:19:53 -0400 Subject: [PATCH] solve 2015 day 17 --- 2015/README.md | 3 ++- 2015/lib/2015/17.ex | 36 ++++++++++++++++++++++++++++++++++++ 2015/mix.exs | 3 ++- 2015/mix.lock | 2 ++ 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 2015/lib/2015/17.ex diff --git a/2015/README.md b/2015/README.md index 9a2933d..0ed2f5b 100644 --- a/2015/README.md +++ b/2015/README.md @@ -16,7 +16,7 @@ | :--: | :--: | :-: | :-: | :-: | :-: | :-: | | | | [1] | [2] | [3] | [4] | [5] | | [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 | | [1]: ./lib/2015/1.ex @@ -35,3 +35,4 @@ [14]: ./lib/2015/14.ex [15]: ./lib/2015/15.ex [16]: ./lib/2015/16.ex +[17]: ./lib/2015/17.ex diff --git a/2015/lib/2015/17.ex b/2015/lib/2015/17.ex new file mode 100644 index 0000000..26f1de9 --- /dev/null +++ b/2015/lib/2015/17.ex @@ -0,0 +1,36 @@ +import AOC + +aoc 2015, 17 do + def input() do + input_stream() + |> Stream.map(&String.to_integer/1) + |> Enum.sort(:desc) + end + + def p1 do + input() + |> combinations() + |> Enum.filter(fn comb -> Enum.sum(comb) == 150 end) + |> length() + end + + def p2 do + working_combinations = + input() + |> combinations() + |> Enum.filter(fn comb -> Enum.sum(comb) == 150 end) + |> Enum.sort_by(&length/1) + + size_of_fewest_container_combination = working_combinations |> hd() |> length() + + working_combinations + |> Enum.count(&(length(&1) == size_of_fewest_container_combination)) + end + + def combinations(values, combination \\ []) + def combinations([], combination), do: [combination] + + def combinations([x | xs], combination) do + combinations(xs, [x | combination]) ++ combinations(xs, combination) + end +end diff --git a/2015/mix.exs b/2015/mix.exs index 070dc2d..10923e9 100644 --- a/2015/mix.exs +++ b/2015/mix.exs @@ -26,7 +26,8 @@ defmodule AdventOfCode2015.MixProject do {:jason, "~> 1.2"}, {:libgraph, "~> 0.13.3"}, {:nimble_parsec, "~> 1.0"}, - {:nx, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "nx", override: true} + {:nx, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "nx", override: true}, + {:rexbug, ">= 1.0.0"} ] end end diff --git a/2015/mix.lock b/2015/mix.lock index 84c18b9..77a5f35 100644 --- a/2015/mix.lock +++ b/2015/mix.lock @@ -6,5 +6,7 @@ "libgraph": {:hex, :libgraph, "0.13.3", "20732b7bafb933dcf7351c479e03076ebd14a85fd3202c67a1c197f4f7c2466b", [:mix], [], "hexpm", "78f2576eef615440b46f10060b1de1c86640441422832052686df53dc3c148c6"}, "nimble_parsec": {:hex, :nimble_parsec, "1.2.0", "b44d75e2a6542dcb6acf5d71c32c74ca88960421b6874777f79153bbbbd7dccc", [:mix], [], "hexpm", "52b2871a7515a5ac49b00f214e4165a40724cf99798d8e4a65e4fd64ebd002c1"}, "nx": {:git, "https://github.com/elixir-nx/nx.git", "e23a678bf0ebcbbafe03f1b5ed78623f052ad486", [sparse: "nx"]}, + "redbug": {:hex, :redbug, "1.2.2", "366d8961770ddc7bb5d209fbadddfa7271005487f938c087a0e385a57abfee33", [:rebar3], [], "hexpm", "b5fe7b94e487be559cb0ec1c0e938c9761205d3e91a96bf263bdf1beaebea729"}, + "rexbug": {:hex, :rexbug, "1.0.5", "e4fce59d1cb4f574b2d84181507b4782bc4b6afcb64e2cd276003c563ffef766", [:mix], [{:mix_test_watch, ">= 0.5.0", [hex: :mix_test_watch, repo: "hexpm", optional: true]}, {:redbug, "~> 1.2", [hex: :redbug, repo: "hexpm", optional: false]}], "hexpm", "13a3f180a9e490686a774725a07a21caf05735b7c012824d86960e9541aab46a"}, "xla": {:hex, :xla, "0.2.0", "689887888afb22587168d461f0e9ff83d7b06040273ea7082dbf9ff7eca33dcc", [:make, :mix], [{:elixir_make, "~> 0.4", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "a2e7b81413db49a159eabfb12dbd784a7c04b5c68c7b4057238d5ec9b110f2ec"}, }