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

Day 15

day15.livemd

Day 15

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 Day15 do
  @parser ~r/(\w+): capacity (-?\d+), durability (-?\d+), flavor (-?\d+), texture (-?\d+), calories (-?\d+)/
  # Hard to imagine that anything is outside the range centered on 25%
  # @sensible_range 15..35
  @sensible_range 0..100
  @tsp_budget 100
  @calorie_budget 500

  def parse_ingredients(text) do
    text
    |> AOC.as_single_lines()
    |> Enum.reduce(%{}, fn line, acc ->
      [[_, ingredient, capacity, durability, flavor, texture, calories]] = 
        Regex.scan(@parser, line)
      [capacity, durability, flavor, texture, calories] = Enum.map(
        [capacity, durability, flavor, texture, calories], &String.to_integer/1
      )
      Map.put(acc, ingredient, %{
        capacity: capacity, 
        durability: durability, 
        flavor: flavor, 
        texture: texture,
        calories: calories})
    end)
  end

  def solve1(text) do
    ingredients = parse_ingredients(text)
    |> IO.inspect(label: "ingredients")

    # example
    # %{"Butterscotch" => 44, "Cinnamon" => 56} 
    # puzzle input
    %{"Butterscotch" => 31, "Candy" => 29, "Frosting" => 24, "Sugar" => 16} # 
    # %{"Butterscotch" => 29, "Candy" => 24, "Frosting" => 22, "Sugar" => 24} # wrong for part2
    |> Enum.map(fn {name, tsp} ->
      [tsp * ingredients[name].capacity, 
        tsp * ingredients[name].durability, 
        tsp * ingredients[name].flavor, 
        tsp * ingredients[name].texture]
      |> IO.inspect(label: name)
    end)
    |> Enum.zip()
    |> Enum.map(fn tuple -> 
      Tuple.sum(tuple)
    end)
    |> Enum.reduce(1, fn sum, acc ->
      (acc * (max(0, sum) |> IO.inspect()))
    end)
  end

  def score(ingredients, amounts) do
    scores = amounts
      |> Enum.map(fn {name, tsp} ->
        [tsp * ingredients[name].capacity, 
          tsp * ingredients[name].durability, 
          tsp * ingredients[name].flavor, 
          tsp * ingredients[name].texture, 
          tsp * ingredients[name].calories]
        # |> IO.inspect(label: name)
      end)
      |> Enum.zip()
      |> Enum.map(fn tuple -> 
        Tuple.sum(tuple)
      end)
      # |> IO.inspect(label: "with calories")
    
    # remove calories
    [calories | rest] = Enum.reverse(scores)
    # IO.inspect(rest, label: "with calories #{calories}")
    if calories != @calorie_budget do
      # IO.inspect([rest, amounts], label: "WRONG calories #{calories}")
      0
    else
      result = rest
        |> Enum.reduce(1, fn sum, acc ->
          (acc * max(0, sum))
        end)
      if result > 0 do
        IO.inspect([Map.values(amounts), result, rest], label: "500 calories")
        result
      else
        0
      end
    end
  end

  def trial_and_error(ingredients, to_try, amounts \\ %{}, budget \\ @tsp_budget)
  # too much ingredients
  def trial_and_error(_ingredients, _to_try, _amounts, budget) when budget < 0, do: []
  # Completion
  def trial_and_error(ingredients, _to_try = [], amounts, _budget) do
    result = score(ingredients, amounts)
    if result > 0 do
      [{result, amounts}]
    else
      []
    end
  end
  # Entry point
  def trial_and_error(ingredients, to_try, amounts = %{}, budget) do
    Enum.reduce(to_try, [], fn next_ingredient, outer_list -> 
      Enum.reduce(@sensible_range, outer_list, fn amount, inner_list ->
        inner_list ++ trial_and_error(ingredients, to_try -- [next_ingredient], 
          Map.put(amounts, next_ingredient, amount), budget - amount)
      end)        
    end)
  end

  def solve2(text) do
    ingredients = parse_ingredients(text)

    # part1solution = %{"Butterscotch" => 31, "Candy" => 29, "Frosting" => 24, "Sugar" => 16}
    # IO.inspect([part1solution, score(ingredients, part1solution)])
    
    trial_and_error(ingredients, Map.keys(ingredients) |> Enum.sort())
    |> Enum.uniq()
    |> Enum.sort_by(fn {score, _details} -> score end)
    |> Enum.reverse()
    |> Enum.take(5)
    
    # %{"Butterscotch" => 27, "Candy" => 23, "Frosting" => 26, "Sugar" => 24 } # 500 (but 14902272 is too low)
  end
end

# Example data:

p1data.()
|> Day15.solve1()
|> IO.inspect(label: "\n*** Part 1 solution (example: 2)")
# 18965440 by trial and error

p1data.()
|> Day15.solve2()
|> IO.inspect(label: "\n*** Part 2 solution (example: 2)")
# 14902272 is too low
# 15711936 is still wrong
# After fixing an off-by-one error:
# 15862900 %{"Butterscotch" => 31, "Candy" => 23, "Frosting" => 21, "Sugar" => 25}