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

Day 17

day17.livemd

Day 17

Mix.install([
  {:kino, "~> 0.14.2"}
])

IEx.Helpers.c("/Users/johnb/dev/2015adventOfCode/advent_of_code.ex")
alias AdventOfCode, as: AOC
alias Kino.Input
require Integer

# Note: when making the next template, something like this works well:
#   `cat day01.livemd | sed 's/01/02/' > day02.livemd`
#

Installation and Data

input_p1example = Kino.Input.textarea("Example Data")
input_p1puzzleInput = Kino.Input.textarea("Puzzle Input")
input_source_select =
  Kino.Input.select("Source", [{:example, "example"}, {:puzzle_input, "puzzle input"}])
p1data = fn ->
  (Kino.Input.read(input_source_select) == :example &&
     Kino.Input.read(input_p1example)) ||
    Kino.Input.read(input_p1puzzleInput)
end

Part 1

defmodule Day17 do
  def filter_inspect(list, result) do
    list = Enum.reject(list, fn item -> item =~ "0x" end)
    # IO.inspect(list)
    {_, updated} = Map.get_and_update(result, Enum.count(list), fn current ->
        {current, (current || 0) + 1}
      end)
    updated 
    # |> IO.inspect(label: "updated")
  end
  def count_permutations(max, _sizes, _steps, result) when max < 0, do: result
  def count_permutations(0, _sizes, steps, result) do
    filter_inspect(steps, result)
  end
  def count_permutations(max, [last] = _sizes, steps, result) do
    if max == last do
      filter_inspect(steps ++ ["1x#{last}"], result)
    else
      result
    end
  end
  def count_permutations(max, [first | rest] = _sizes, steps, result) do
    Enum.reduce(0..1, result, fn count, acc ->
      count_permutations(max - first * count, rest, steps ++ ["#{count}x#{first}"], acc)
    end)
  end

  def parse_sizes(text) do
    text
    |> AOC.as_integers()
    |> Enum.sort()
    |> Enum.reverse()
    |> IO.inspect(label: "sizes")
  end

  def solve1(text) do
    sizes = parse_sizes(text)
    max = (Enum.count(sizes) == 5) &amp;&amp; 25 || 150
    
    count_permutations(max, sizes, [], %{})
      |> Map.values()
      |> Enum.sum()
  end

  def solve2(text) do
    sizes = parse_sizes(text)
    max = (Enum.count(sizes) == 5) &amp;&amp; 25 || 150
    
    permutes = count_permutations(max, sizes, [], %{})
    smallest_key = Enum.min(Map.keys(permutes)) |> IO.inspect(label: "smallest_key")
    permutes[smallest_key]
  end
end

# Example data:

p1data.()
|> Day17.solve1()
|> IO.inspect(label: "\n*** Part 1 solution (example: 4)")
# 1304

p1data.()
|> Day17.solve2()
|> IO.inspect(label: "\n*** Part 2 solution (example: 3)")
# 18