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

Day 08

day08.livemd

Day 08

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/08/04/' > day04.livemd`
#
# When inspecting lists of numbers, use "charlists: :as_lists"

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

defmodule Day08 do
  @ground "."
  @extra_wide 2000
  
  def parse(text) do
    grid = AOC.as_grid(text, @extra_wide)
    IO.inspect(Enum.count(AOC.grid_cells(grid)), label: "AOC.grid_cells(grid)")

    nodes = AOC.grid_cells(grid)
      |> Enum.reject(fn cell -> is_nil(grid[cell]) || grid[cell] == @ground end)
      |> Enum.reduce(%{}, fn cell, acc ->
        frequency = grid[cell]
        Map.put(acc, frequency, (acc[frequency] || []) ++ [cell])
      end)
      # |> IO.inspect(charlists: :as_lists)
    
    {grid, nodes}
  end

  # Misnomer: actually finds both antinodes, regardless of onboard or not
  def onboard_antinodes(_grid, [_location]), do: []
  def onboard_antinodes(grid, [first | rest] = _locations) do
    (Enum.map(rest, fn node ->
      delta = first - node
      [first + delta, node - delta]
    end) ++ onboard_antinodes(grid, rest))
    # |> IO.inspect(charlists: :as_lists, label: "antinodes")
    |> List.flatten()
  end

  def find_antinodes(grid, nodes) do
    Enum.reduce(nodes, %{}, fn {frequency, locations}, acc ->
      Map.put(acc, frequency, (acc[frequency] || []) ++ 
        onboard_antinodes(grid, locations)
      )
    end)
  end

  def all_antinodes(_grid, [_location]), do: []
  def all_antinodes(grid, [first | rest] = _locations) do
    (Enum.map(rest, fn node ->
      delta = first - node
      [
        first, node,
        Enum.map(1..grid.grid_width, fn iteration -> first + iteration * delta end),
        Enum.map(1..grid.grid_width, fn iteration -> node - iteration * delta end)
      ]
    end) ++ all_antinodes(grid, rest))
    # |> IO.inspect(charlists: :as_lists, label: "antinodes")
    |> List.flatten()
  end

  def find_all_antinodes(grid, nodes) do
    Enum.reduce(nodes, %{}, fn {frequency, locations}, acc ->
      Map.put(acc, frequency, (acc[frequency] || []) ++ 
        all_antinodes(grid, locations)
      )
    end)
  end

  def solve1(text) do
    {grid, nodes} = parse(text)
    antinodes = find_antinodes(grid, nodes)

    Map.values(antinodes)
    |> List.flatten()
    |> Enum.uniq()
    |> Enum.reject(fn cell -> is_nil(grid[cell]) end)
    |> Enum.count()
  end

  def solve2(text) do
    {grid, nodes} = parse(text)
    antinodes = find_all_antinodes(grid, nodes)

    Map.values(antinodes)
    |> List.flatten()
    |> Enum.uniq()
    |> Enum.reject(fn cell -> is_nil(grid[cell]) end)
    |> Enum.count()
  end
end

# Example:

p1data.()
|> Day08.solve1()
|> IO.inspect(label: "\n*** Part 1 solution (example: 14)")
# 308

p1data.()
|> Day08.solve2()
|> IO.inspect(label: "\n*** Part 2 solution (example: 34)")
# 1147