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

Day 4: Camp Cleanup

2022/day-04.livemd

Day 4: Camp Cleanup

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

Day 4

sample_input = Kino.Input.textarea("Paste Sample Input")
real_input = Kino.Input.textarea("Paste Real Input")
parse_single_assignment = fn spec ->
  spec
  |> String.split("-")
  |> Enum.map(&String.to_integer/1)
  |> List.to_tuple()
end

parse_assignments = fn input ->
  input
  |> Kino.Input.read()
  |> String.split()
  |> Enum.map(fn line ->
    line
    |> String.split(",")
    |> Enum.map(parse_single_assignment)
    |> List.to_tuple()
  end)
end

contains? = fn {start_one, end_one}, {start_two, end_two} ->
  start_one <= start_two and end_one >= end_two
end

subset? = fn
  {one, two} -> contains?.(one, two) or contains?.(two, one)
end

overlap? = fn {{start_one, end_one}, {start_two, end_two}} ->
  !(end_one < start_two or start_one > end_two)
end

count_subsets = &amp;Enum.count(&amp;1, subset?)

count_overlap = &amp;Enum.count(&amp;1, overlap?)
sample_input |> parse_assignments.() |> count_subsets.()
real_input |> parse_assignments.() |> count_subsets.()
sample_input |> parse_assignments.() |> count_overlap.()
real_input |> parse_assignments.() |> count_overlap.()