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

Advent of Code 2022 - Day 04

livebooks/day_04.livemd

Advent of Code 2022 - Day 04

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

Setup

example_input = """
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8
"""

input = Kino.Input.textarea("Puzzle Input", default: example_input)

Part 1

elf_pairs =
  input
  |> Kino.Input.read()
  |> String.split("\n", trim: true)
  |> Enum.map(fn raw_elf_pair ->
    raw_elf_pair
    |> String.split(",")
    |> Enum.map(fn elf ->
      [first, last] = String.split(elf, "-")

      MapSet.new(String.to_integer(first)..String.to_integer(last))
    end)
  end)

Enum.count(elf_pairs, fn [first_elf, second_elf] ->
  MapSet.subset?(first_elf, second_elf) or MapSet.subset?(second_elf, first_elf)
end)

Part 2

Enum.count(elf_pairs, fn [first_elf, second_elf] ->
  not MapSet.disjoint?(first_elf, second_elf)
end)