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

Day 23

day23.livemd

Day 23

# Note: when making the next template, something like this works well:
#   `cat day04.livemd | sed 's/23/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 Day23 do
  def parse(text) do
    text
    |> AOC.as_single_lines()
    |> Enum.map(fn line -> String.split(line, "-", trim: true) end)
    |> Enum.reduce(%{}, fn [a,b], acc ->
      acc
      |> add_connection(a, b)
      |> add_connection(b, a)
    end)
  end

  def add_connection(map, a, b) do
    get_and_update_in(map, [a], fn current ->
      cond do
        is_nil(current) ->
          {current, MapSet.new([b])}
        true ->
          {current, MapSet.put(current, b)}
      end
    end)
    |> elem(1)
  end

  def three_way_connections(connections, computers) do
    computers
    |> Enum.map(fn computer ->
      duped = for other1 <- connections[computer], other2 <- connections[computer], 
        other1 != other2 &amp;&amp; 
          MapSet.member?(connections[other1], other2) &amp;&amp;
          MapSet.member?(connections[other2], other1),
        do: [computer, other1, other2] |> Enum.sort()

      duped
      # conversion to a string is necessary so a later 
      # flatten operation won't wipe out the grouping.
      |> Enum.map(fn list -> Enum.join(list, ",") end)
      |> Enum.uniq()
    end)
  end

  def solve1(text) do
    connections = parse(text)
    tees = Map.keys(connections)    
      |> Enum.filter(fn key -> String.starts_with?(key, "t") end)

    three_way_connections(connections, tees)
    |> List.flatten()
    |> Enum.uniq()
    |> Enum.sort()
    |> Enum.count()
  end

  def solve2(text) do
    connections = parse(text)

    three_way_connections(connections, Map.keys(connections))
    |> Enum.sort_by(fn list -> Enum.count(list) end, :desc)
    |> List.first()
    |> List.flatten()
    # ungroup so we can extract the individual computers
    |> Enum.map(fn csv -> String.split(csv, ",", trim: true) end)
    |> List.flatten()
    |> Enum.uniq()
    |> Enum.sort()
    |> Enum.join(",")
  end
end

# Example:

IO.inspect(Time.utc_now())
data.()
|> Day23.solve1()
|> IO.inspect(label: "\n*** Part 1 solution (example: 7)")
IO.inspect(Time.utc_now())
# 1253

IO.inspect(Time.utc_now())
data.()
|> Day23.solve2()
|> IO.inspect(label: "\n*** Part 2 solution (example: 'co,de,ka,ta')")
IO.inspect(Time.utc_now())
# ag,bt,cq,da,hp,hs,mi,pa,qd,qe,qi,ri,uq