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

Day 05

day05.livemd

Day 05

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

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

# Note: when making the next template, something like this works well:
#   `cat day04.livemd | sed 's/05/04/' > day04.livemd`
#

Installation and Data

input_p1example = Kino.Input.textarea("Example Data", monospace: true)
input_p1puzzleInput = Kino.Input.textarea("Puzzle Input", monospace: true)
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

Solution

# When inspecting lists of numbers, use "charlists: :as_lists"
defmodule Day05 do
  def parse(text) do
    [rules, updates] = AOC.as_doublespaced_paragraphs(text)
    rules = rules
      |> AOC.as_single_lines()
      |> Enum.reduce(%{}, fn line, acc ->
        [first, second] = String.split(line, "|")
          |> Enum.map(&String.to_integer/1)

        {_, acc} = get_and_update_in(acc[first], fn current ->
          {current, (current || []) ++ [second]} 
        end)
          
        acc
      end)

    updates = updates
      |> AOC.as_single_lines()
      |> Enum.map(&AOC.as_comma_separated_integers/1)

    [rules, updates]
  end

  def solve1(text) do
    [rules, updates] = parse(text)

    updates
    |> Enum.filter(fn progression ->
      progression == Enum.sort(progression, fn a, b -> !(a in (rules[b] || [])) end)
    end)
    |> Enum.map(fn progression ->
      {center, _others} = List.pop_at(progression, floor(Enum.count(progression) / 2))
      center
    end)
    |> Enum.sum()
  end

  def solve2(text) do
    [rules, updates] = parse(text)

    updates
    |> Enum.reject(fn progression ->
      progression == Enum.sort(progression, fn a, b -> !(a in (rules[b] || [])) end)
    end)
    |> Enum.map(fn progression ->
      Enum.sort(progression, fn a, b -> !(a in (rules[b] || [])) end)
    end)
    |> Enum.map(fn progression ->
      {center, _others} = List.pop_at(progression, floor(Enum.count(progression) / 2))
      center
    end)
    |> Enum.sum()
  end
end

# Example:

p1data.()
|> Day05.solve1()
|> IO.inspect(label: "\n*** Part 1 solution (example: 143)")
# 4790

p1data.()
|> Day05.solve2()
|> IO.inspect(label: "\n*** Part 2 solution (example: 123)")
# 6319