Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Advent of Code 2015 Day 17 Part 1

2015_day17_part1.livemd

Advent of Code 2015 Day 17 Part 1

Mix.install([
  {:kino_aoc, "~> 0.1"}
])

Get Inputs

{:ok, puzzle_input} =
  KinoAOC.download_puzzle("2015", "17", System.fetch_env!("LB_SESSION"))

My answer

containers =
  puzzle_input
  |> String.split("\n")
  |> Enum.map(&String.to_integer(&1))
  |> Enum.sort(:desc)
defmodule Combinations do
  def get(_, n) when n == 0, do: [[]]
  def get([], _), do: []
  def get([head | tail], n) do
    with_head =
      tail
      |> get(n - 1)
      |> Enum.map(fn combo -> [head | combo] end)

    without_head = get(tail, n)

    with_head ++ without_head
  end
end
1..20
|> Enum.map(fn length ->
  containers
  |> Combinations.get(length)
  |> Enum.count(fn combination -> Enum.sum(combination) == 150 end)
end)
|> Enum.sum()