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

Day 4 - Camp Cleanup

day04.livemd

Day 4 - Camp Cleanup

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

Section

input = Kino.Input.textarea("Please insert your input")
sample_input =
  input
  |> Kino.Input.read()
  |> String.split("\n", trim: true)
  |> Enum.map(fn pair ->
    String.split(pair, ",")
    |> Enum.map(fn assignment ->
      String.split(assignment, "-")
      |> Enum.map(&String.to_integer/1)
      |> List.to_tuple()
    end)
  end)
[
  [{2, 5}, {15, 90}],
  [{42, 93}, {43, 57}],
  [{71, 95}, {72, 81}],
  [{19, 92}, {2, 93}],
  [{21, 92}, {57, 93}],
  [{57, 75}, {76, 76}],
  [{66, 92}, {14, 76}],
  [{4, 97}, {5, 96}],
  [{22, 50}, {22, 51}],
  [{10, 43}, {42, 43}],
  [{50, 59}, {60, 60}],
  [{81, 86}, {86, 94}],
  [{86, 98}, {82, 99}],
  [{44, 63}, {44, 62}],
  [{36, 65}, {42, 89}],
  [{2, 86}, {14, 85}],
  [{51, 51}, {48, 52}],
  [{41, 55}, {10, 55}],
  [{3, 99}, {3, 96}],
  [{62, 64}, {38, 63}],
  [{24, 80}, {80, 98}],
  [{13, 67}, {7, 47}],
  [{22, 78}, {21, 91}],
  [{3, 92}, {26, 91}],
  [{97, 98}, {86, 98}],
  [{17, 90}, {16, 91}],
  [{4, 96}, {97, 97}],
  [{33, 46}, {44, 47}],
  [{98, 98}, {44, 96}],
  [{9, 36}, {7, 13}],
  [{17, 91}, {18, 90}],
  [{1, 37}, {4, 37}],
  [{19, 58}, {18, 58}],
  [{42, 43}, {11, 42}],
  [{9, 98}, {10, 97}],
  [{6, 22}, {5, 6}],
  [{9, 12}, {13, 61}],
  [{4, 16}, {17, 24}],
  [{37, 45}, {36, 38}],
  [{62, 86}, {61, 61}],
  [{8, 41}, {8, 40}],
  [{35, 91}, {35, 98}],
  [{84, 85}, {72, 84}],
  [{40, 90}, {41, 91}],
  [{47, 58}, {31, 59}],
  [{30, 80}, {22, 31}],
  [{87, 89}, {58, ...}],
  [{13, ...}, {...}],
  [{...}, ...],
  [...],
  ...
]
sample_input
|> Enum.filter(fn [{a, b}, {c, d}] ->
  (a <= c &amp;&amp; b >= d) || (c <= a &amp;&amp; d >= b)
end)
|> Enum.count()
462

Part 2

    a            b
    |------------|
        |-------------|
        c             d

             a           b
             |-----------|
    |-------------|
    c             d
  • a <= c && b >= c
  • c <= a && b >= d
sample_input
|> Enum.filter(fn [{a, b}, {c, d}] ->
  (a <= c &amp;&amp; c <= b) ||
    (c <= a &amp;&amp; a <= d)
end)
|> Enum.count()
835