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 all(_, 0), do: [[]]
  def all([], _), do: []
  def all(list, n) when length(list) == n, do: [list]

  def all([head | tail], n) do
    with_head = for combo <- all(tail, n - 1), do: [head | combo]
    without_head = all(tail, n)
    with_head ++ without_head
  end
end
1..20
|> Enum.map(fn length ->
  containers
  |> Combinations.all(length)
  |> Enum.count(fn combination -> Enum.sum(combination) == 150 end)
end)
|> Enum.sum()