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

Day 18

day18.livemd

Day 18

# Note: when making the next template, something like this works well:
#   `cat day04.livemd | sed 's/18/04/' > day04.livemd`
# When inspecting lists of numbers, use "charlists: :as_lists"
#
Mix.install([
  # Join the string so a copy of dayN to dayM doesn't destroy it.
  {:kino, "~> 0.1" <> "4.2"}
])

# Join the string so a copy of dayN to dayM doesn't destroy it.
IEx.Helpers.c("/Users/johnb/dev/2" <> "0" <> "2" <> "4adventOfCode/advent_of_code.ex")
alias AdventOfCode, as: AOC
alias Kino.Input

Installation and Data

input_example = Kino.Input.textarea("Example Data", monospace: true)
input_puzzleInput = Kino.Input.textarea("Puzzle Input", monospace: true)
input_source_select =
  Kino.Input.select("Source", [{:example, "example"}, {:puzzle_input, "puzzle input"}])
data = fn ->
  (Kino.Input.read(input_source_select) == :example &amp;&amp;
     Kino.Input.read(input_example)) ||
    Kino.Input.read(input_puzzleInput)
end

Solution

defmodule Day18 do
  def parse(text) do
    text
    |> AOC.as_single_lines()
    |> Enum.map(&amp;AOC.as_comma_separated_integers/1)
    |> Enum.sort_by(fn [a, b] -> [b, a] end)
  end

  def solve1(text) do
    dropped_bytes = parse(text)
    |> AOC.inspect()
    grid_size = (Enum.count(dropped_bytes) < 500) &amp;&amp; 6 || 70
    # dropped_bytes
    grid = AOC.as_grid("\n.\n.\n.\n.\n.\n.\n", grid_size)
    |> AOC.display_grid()
  end

  def solve2(text) do
    parse(text)
  end
end

# Example:

data.()
|> Day18.solve1()
|> IO.inspect(label: "\n*** Part 1 solution (example: )")
#

# data.()
# |> Day18.solve2()
# |> IO.inspect(label: "\n*** Part 2 solution (example: )")
#