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

Advent 2022 - Day 4

day4.livemd

Advent 2022 - Day 4

Mix.install([
  {:kino, github: "livebook-dev/kino"}
])
:ok

Setup

input = Kino.Input.textarea("Please paste your input file:")
sets =
  input
  |> Kino.Input.read()
  |> String.split("\n", trim: true)
  |> Enum.map(fn line ->
    line
    |> String.split(",")
    |> Enum.map(fn pair ->
      pair
      |> String.split("-")
      |> Enum.map(&String.to_integer/1)
      |> Enum.reduce(&Range.new(&1, &2))
      |> MapSet.new()
    end)
  end)
[
  [MapSet.new([2, 3, 4]), MapSet.new([6, 7, 8])],
  [MapSet.new([2, 3]), MapSet.new([4, 5])],
  [MapSet.new([5, 6, 7]), MapSet.new([7, 8, 9])],
  [MapSet.new([2, 3, 4, 5, 6, 7, 8]), MapSet.new([3, 4, 5, 6, 7])],
  [MapSet.new([6]), MapSet.new([4, 5, 6])],
  [MapSet.new([2, 3, 4, 5, 6]), MapSet.new([4, 5, 6, 7, 8])]
]

Part 1

sets
|> Enum.filter(fn [a, b] -> MapSet.subset?(a, b) || MapSet.subset?(b, a) end)
|> Enum.count()
2

Part 2

sets
|> Enum.filter(fn [a, b] -> MapSet.intersection(a, b) |> Enum.count() != 0 end)
|> Enum.count()
4