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

Day 04

books/aoc/2022/04.livemd

Day 04

Mix.install([
  {:kino, "~> 0.6.2"},
  {:kino_aoc, git: "https://github.com/ljgago/kino_aoc"}
])
:ok

Input

{:ok, result} = KinoAOC.download_puzzle("2022", "4", System.fetch_env!("LB_AOC_SESSION"))
input = result
"23-33,24-65\n10-24,23-88\n71-92,18-71\n2-2,10-95\n24-26,25-66\n19-93,36-99\n12-99,5-13\n13-88,14-94\n4-85,5-84\n78-88,79-87\n13-82,13-83\n3-98,3-99\n27-69,28-89\n24-47,23-66\n43-48,29-49\n2-99,2-98\n3-77,21-65\n10-90,89-93\n64-75,63-75\n97-99,1-97\n48-65,49-64\n18-26,25-28\n85-86,26-85\n82-89,69-83\n21-93,93-98\n73-79,46-81\n33-52,32-51\n1-99,99-99\n11-36,34-40\n31-88,32-89\n9-19,18-35\n9-84,83-83\n34-82,4-83\n33-33,24-33\n34-83,83-90\n11-86,10-86\n48-48,3-50\n8-92,9-91\n16-38,15-37\n4-84,5-93\n70-75,41-79\n40-48,47-92\n37-41,52-89\n3-3,4-91\n27-77,27-77\n64-90,33-52\n28-40,9-64\n2-73,3-95\n17-94,7-75\n6-71,7-71\n11-69,10-70\n4-40,4-40\n9-54,30-44\n92-93,4-92\n62-63,63-67\n7-34,16-34\n54-55,31-54\n12-90,11-91\n11-13,12-53\n10-73,9-64\n13-18,13-84\n6-82,16-81\n52-78,18-53\n22-82,23-74\n90-91,3-90\n92-93,70-93\n18-44,19-45\n20-20,20-67\n31-93,76-93\n25-86,86-86\n6-73,5-94\n75-84,74-86\n50-51,50-51\n56-86,57-86\n18-81,13-18\n3-9,8-93\n31-36,34-34\n19-88,18-87\n10-95,54-79\n83-85,66-86\n32-68,31-49\n14-97,12-99\n42-42,42-53\n27-32,28-69\n96-98,15-96\n25-49,25-48\n71-81,15-92\n19-83,15-19\n14-99,6-14\n52-90,53-94\n5-98,85-96\n18-30,10-15\n4-86,4-98\n54-55,30-78\n85-99,62-86\n10-99,10-98\n4-96,5-95\n6-59,7-18\n6-86,1-7\n28-85,5-84\n3-82,2-82\n73-92,87-87\n2-3,2-89\n4-5,4-8\n21-96,69-97\n89-90,4-89\n28-79,29-80\n35-58,40-48\n96-96,3-97\n17-27,16-96\n7-99,8-94\n26-92,25-92\n17-24,17-25\n44-44,40-47\n57-64,57-65\n60-91,59-79\n23-79,22-71\n34-37,31-70\n22-60,22-59\n44-99,98-98\n9-57,49-55\n61-64,33-62\n15-68,4-15\n53-55,34-55\n13-13,9-15\n2-43,3-81\n17-56,16-99\n1-4,3-92\n76-77,73-83\n2-97,2-96\n44-45,44-55\n8-89,8-97\n23-73,22-88\n23-90,79-93\n23-87,86-91\n3-45,17-44\n7-99,8-98\n92-97,14-92\n85-89,59-90\n7-8,7-89\n5-90,49-89\n28-30,29-63\n38-61,38-60\n32-98,5-75\n22-87,71-76\n5-85,5-88\n16-96,18-88\n39-51,40-90\n21-21,19-23\n22-55,15-56\n1-75,35-59\n13-84,12-85\n11-83,23-27\n11-98,11-97\n35-92,34-92\n8-96,8-30\n48-72,33-60\n9-83,10-82\n97-97,10-98\n5-7,9-98\n1-44,3-43\n7-85,8-85\n12-32,12-52\n1-86,86-86\n3-63,4-63\n3-83,83-91\n74-88,38-74\n92-95,6-98\n11-19,10-20\n6-94,7-94\n39-90,38-42\n60-92,61-92\n43-94,44-98\n23-98,22-99\n31-41,27-32\n9-79,9-78\n63-64,11-64\n82-82,83-98\n78-79,77-78\n19-63,4-27\n26-62,27-44\n91-91,91-93\n16-94,31-93\n23-25,24-99\n78-79,11-78\n30-30,29-31\n34-85,35-84\n4-78,4-5\n42-84,43-85\n39-87,40-86\n49-84,50-84\n67-67,7-67\n38-38,62-67\n2-83,3-82\n14-33,32-68\n18-76,17-75\n68-77,68-76\n4-97,53-73\n53-97,54-96\n53-55,54-98\n35-95,75-83\n2-3,2-78\n3-97,15-96\n26-87,26-92\n4-10,9-48\n3-89,22-96\n10-85,10-84\n1-93,2-2\n24-37,37-62\n32-68,48-53\n33-96,95-97\n9-26,14-31\n16-66,13-14\n85-86,78-85\n65-82,64-81\n5-85,31-49\n47-88,59-91\n34-71,70-76\n14-14,14-14\n45-46,45-97\n63-73,63-64\n16-54,17-53\n87-88,51-87\n20-79,19-98\n3-96,2-95\n86-86,60-86\n82-82,67-82\n12-35,11-35\n15-31,26-52\n1-2,1-99\n3-96,2-2\n91-97,11-92\n41-89,42-96\n62-64,41-65\n54-54,16-55\n28-41,40-97\n15-89,89-90\n50-71,55-70\n30-66,30-93\n89-92,84-89\n25-27,26-84\n59-91,60-91\n4-11,4-98\n5-48,10-44\n53-80,77-79\n20-21,10-20\n14-86,14-85\n32-70,33-69\n94-94,26-95\n5-57,5-97\n38-50,38-92\n48-50,49-51\n13-98,12-98\n90-95,24-88\n90-94,67-91\n1-11,10-91\n48-59,59-91\n26-68,31-55\n15-56,21-57\n6-69,7-69\n6-8,5-95\n32-93,33-94\n13-73,14-42\n1-4,3-98\n30-62,29-62\n57-64,61-99\n12-27,11-46\n28-99,98-98\n7-83,8-92\n56-77,56-76\n30-89,31-51\n69-83,37-72\n11-78,11-65\n61-61,60-62\n38-65,65-66\n20-44,21-97\n39-76,40-75\n10-94,11-93\n33-90,89-96\n23-77,22-78\n63-86,62-73\n42-81,35-43\n34-91,34-55\n12-97,4-13\n1-98,1-97\n23-72,24-73\n14-99,13-99\n17-97,18-96\n65-85,14-66\n68-77,67-69\n8-96,9-9\n28-57,29-56\n14-26,14-26\n6-72,6-72\n49-49,1-48\n4-65,65-65\n53-92,54-91\n48-87,15-48\n51-54,20-72\n65-75,64-64\n4-92,3-93\n42-70,30-98\n15-71,14-15\n29-62,28-63\n6-92,6-93\n37-84,83-83\n1-99,23-98\n23-82,24-88\n2-60,61-74\n3-5,17-93\n50-63,51-62\n51-55,52-52\n30-82,5-83\n5-77,4-42\n44-99,3-99\n18-81,5-18\n2-32,32-32\n50-59,58-87\n23-30,24-40\n70-70,9-71\n40-62,49-62\n2-29,1-3\n48-69,48-49\n48-48,47-93\n5-42,17-41\n11-95,12-92\n9-66,8-65\n32-84,33-84\n16-69,15-99\n12-96,9-12\n69-91,68-70\n14-69,15-81\n96-99,50-96\n35-62,35-61\n6-22,22-60\n20-76,73-74\n4-21,4-22\n22-30,21-76\n5-90,1-6\n82-96,82-95\n18-31,19-71\n29-56,50-59\n11-21,1-61\n15-76,30-81\n1-1,2-83\n11-90,90-91\n40-90,40-41\n68-83,68-83\n25-81,25-81\n87-88,22-87\n31-65,32-64\n17-90,16-90\n11-65,11-12\n35-60,36-93\n5-93,4-94\n28-29,27-75\n12-38,37-45\n39-46,32-" <> ...

Part 1

defmodule Assignment do
  defp in_range(first, second) do
    Enum.at(first, 0) <= Enum.at(second, 0) and Enum.at(first, 1) >= Enum.at(second, 1)
  end

  def contains?(first, second) do
    cond do
      in_range(first, second) or in_range(second, first) -> true
      true -> false
    end
  end
end

defmodule PartOne do
  def resolve(input) do
    IO.puts("--- Part One ---")
    IO.puts("Result: #{run(input)}")
  end

  def run(input) do
    input
    |> String.split("\n", trim: true)
    |> Enum.map(fn x ->
      String.split(x, ",", trim: true)
      |> Enum.map(fn x -> String.split(x, "-", trim: true) end)
      |> Enum.map(fn x ->
        Enum.map(x, fn x ->
          String.to_integer(x)
        end)
      end)
    end)
    |> Enum.count(fn x ->
      Assignment.contains?(Enum.at(x, 0), Enum.at(x, 1))
    end)
  end
end
{:module, PartOne, <<70, 79, 82, 49, 0, 0, 13, ...>>, {:run, 1}}
input
|> PartOne.resolve()
--- Part One ---
[projects/elixir/aoc/2022/04.livemd#cell:5mej7fxoqmm4f4vdpvlu3cm3i6pchczx:23: PartOne.run/1]
input #=> "23-33,24-65\n10-24,23-88\n71-92,18-71\n2-2,10-95\n24-26,25-66\n19-93,36-99\n12-99,5-13\n13-88,14-94\n4-85,5-84\n78-88,79-87\n13-82,13-83\n3-98,3-99\n27-69,28-89\n24-47,23-66\n43-48,29-49\n2-99,2-98\n3-77,21-65\n10-90,89-93\n64-75,63-75\n97-99,1-97\n48-65,49-64\n18-26,25-28\n85-86,26-85\n82-89,69-83\n21-93,93-98\n73-79,46-81\n33-52,32-51\n1-99,99-99\n11-36,34-40\n31-88,32-89\n9-19,18-35\n9-84,83-83\n34-82,4-83\n33-33,24-33\n34-83,83-90\n11-86,10-86\n48-48,3-50\n8-92,9-91\n16-38,15-37\n4-84,5-93\n70-75,41-79\n40-48,47-92\n37-41,52-89\n3-3,4-91\n27-77,27-77\n64-90,33-52\n28-40,9-64\n2-73,3-95\n17-94,7-75\n6-71,7-71\n11-69,10-70\n4-40,4-40\n9-54,30-44\n92-93,4-92\n62-63,63-67\n7-34,16-34\n54-55,31-54\n12-90,11-91\n11-13,12-53\n10-73,9-64\n13-18,13-84\n6-82,16-81\n52-78,18-53\n22-82,23-74\n90-91,3-90\n92-93,70-93\n18-44,19-45\n20-20,20-67\n31-93,76-93\n25-86,86-86\n6-73,5-94\n75-84,74-86\n50-51,50-51\n56-86,57-86\n18-81,13-18\n3-9,8-93\n31-36,34-34\n19-88,18-87\n10-95,54-79\n83-85,66-86\n32-68,31-49\n14-97,12-99\n42-42,42-53\n27-32,28-69\n96-98,15-96\n25-49,25-48\n71-81,15-92\n19-83,15-19\n14-99,6-14\n52-90,53-94\n5-98,85-96\n18-30,10-15\n4-86,4-98\n54-55,30-78\n85-99,62-86\n10-99,10-98\n4-96,5-95\n6-59,7-18\n6-86,1-7\n28-85,5-84\n3-82,2-82\n73-92,87-87\n2-3,2-89\n4-5,4-8\n21-96,69-97\n89-90,4-89\n28-79,29-80\n35-58,40-48\n96-96,3-97\n17-27,16-96\n7-99,8-94\n26-92,25-92\n17-24,17-25\n44-44,40-47\n57-64,57-65\n60-91,59-79\n23-79,22-71\n34-37,31-70\n22-60,22-59\n44-99,98-98\n9-57,49-55\n61-64,33-62\n15-68,4-15\n53-55,34-55\n13-13,9-15\n2-43,3-81\n17-56,16-99\n1-4,3-92\n76-77,73-83\n2-97,2-96\n44-45,44-55\n8-89,8-97\n23-73,22-88\n23-90,79-93\n23-87,86-91\n3-45,17-44\n7-99,8-98\n92-97,14-92\n85-89,59-90\n7-8,7-89\n5-90,49-89\n28-30,29-63\n38-61,38-60\n32-98,5-75\n22-87,71-76\n5-85,5-88\n16-96,18-88\n39-51,40-90\n21-21,19-23\n22-55,15-56\n1-75,35-59\n13-84,12-85\n11-83,23-27\n11-98,11-97\n35-92,34-92\n8-96,8-30\n48-72,33-60\n9-83,10-82\n97-97,10-98\n5-7,9-98\n1-44,3-43\n7-85,8-85\n12-32,12-52\n1-86,86-86\n3-63,4-63\n3-83,83-91\n74-88,38-74\n92-95,6-98\n11-19,10-20\n6-94,7-94\n39-90,38-42\n60-92,61-92\n43-94,44-98\n23-98,22-99\n31-41,27-32\n9-79,9-78\n63-64,11-64\n82-82,83-98\n78-79,77-78\n19-63,4-27\n26-62,27-44\n91-91,91-93\n16-94,31-93\n23-25,24-99\n78-79,11-78\n30-30,29-31\n34-85,35-84\n4-78,4-5\n42-84,43-85\n39-87,40-86\n49-84,50-84\n67-67,7-67\n38-38,62-67\n2-83,3-82\n14-33,32-68\n18-76,17-75\n68-77,68-76\n4-97,53-73\n53-97,54-96\n53-55,54-98\n35-95,75-83\n2-3,2-78\n3-97,15-96\n26-87,26-92\n4-10,9-48\n3-89,22-96\n10-85,10-84\n1-93,2-2\n24-37,37-62\n32-68,48-53\n33-96,95-97\n9-26,14-31\n16-66,13-14\n85-86,78-85\n65-82,64-81\n5-85,31-49\n47-88,59-91\n34-71,70-76\n14-14,14-14\n45-46,45-97\n63-73,63-64\n16-54,17-53\n87-88,51-87\n20-79,19-98\n3-96,2-95\n86-86,60-86\n82-82,67-82\n12-35,11-35\n15-31,26-52\n1-2,1-99\n3-96,2-2\n91-97,11-92\n41-89,42-96\n62-64,41-65\n54-54,16-55\n28-41,40-97\n15-89,89-90\n50-71,55-70\n30-66,30-93\n89-92,84-89\n25-27,26-84\n59-91,60-91\n4-11,4-98\n5-48,10-44\n53-80,77-79\n20-21,10-20\n14-86,14-85\n32-70,33-69\n94-94,26-95\n5-57,5-97\n38-50,38-92\n48-50,49-51\n13-98,12-98\n90-95,24-88\n90-94,67-91\n1-11,10-91\n48-59,59-91\n26-68,31-55\n15-56,21-57\n6-69,7-69\n6-8,5-95\n32-93,33-94\n13-73,14-42\n1-4,3-98\n30-62,29-62\n57-64,61-99\n12-27,11-46\n28-99,98-98\n7-83,8-92\n56-77,56-76\n30-89,31-51\n69-83,37-72\n11-78,11-65\n61-61,60-62\n38-65,65-66\n20-44,21-97\n39-76,40-75\n10-94,11-93\n33-90,89-96\n23-77,22-78\n63-86,62-73\n42-81,35-43\n34-91,34-55\n12-97,4-13\n1-98,1-97\n23-72,24-73\n14-99,13-99\n17-97,18-96\n65-85,14-66\n68-77,67-69\n8-96,9-9\n28-57,29-56\n14-26,14-26\n6-72,6-72\n49-49,1-48\n4-65,65-65\n53-92,54-91\n48-87,15-48\n51-54,20-72\n65-75,64-64\n4-92,3-93\n42-70,30-98\n15-71,14-15\n29-62,28-63\n6-92,6-93\n37-84,83-83\n1-99,23-98\n23-82,24-88\n2-60,61-74\n3-5,17-93\n50-63,51-62\n51-55,52-52\n30-82,5-83\n5-77,4-42\n44-99,3-99\n18-81,5-18\n2-32,32-32\n50-59,58-87\n23-30,24-40\n70-70,9-71\n40-62,49-62\n2-29,1-3\n48-69,48-49\n48-48,47-93\n5-42,17-41\n11-95,12-92\n9-66,8-65\n32-84,33-84\n16-69,15-99\n12-96,9-12\n69-91,68-70\n14-69,15-81\n96-99,50-96\n35-62,35-61\n6-22,22-60\n20-76,73-74\n4-21,4-22\n22-30,21-76\n5-90,1-6\n82-96,82-95\n18-31,19-71\n29-56,50-59\n11-21,1-61\n15-76,30-81\n1-1,2-83\n11-90,90-91\n40-90,40-41\n68-83,68-83\n25-81,25-81\n87-88,22-87\n31-65,32-64\n17-90,16-90\n11-65,11-12\n35-60,36-93\n5-93,4-94\n28-29,27-75\n12-38,37-45\n39-46,32-" <> ...
|> String.split("\n", trim: true) #=> ["23-33,24-65", "10-24,23-88", "71-92,18-71", "2-2,10-95", "24-26,25-66",
 "19-93,36-99", "12-99,5-13", "13-88,14-94", "4-85,5-84", "78-88,79-87",
 "13-82,13-83", "3-98,3-99", "27-69,28-89", "24-47,23-66", "43-48,29-49",
 "2-99,2-98", "3-77,21-65", "10-90,89-93", "64-75,63-75", "97-99,1-97",
 "48-65,49-64", "18-26,25-28", "85-86,26-85", "82-89,69-83", "21-93,93-98",
 "73-79,46-81", "33-52,32-51", "1-99,99-99", "11-36,34-40", "31-88,32-89",
 "9-19,18-35", "9-84,83-83", "34-82,4-83", "33-33,24-33", "34-83,83-90",
 "11-86,10-86", "48-48,3-50", "8-92,9-91", "16-38,15-37", "4-84,5-93",
 "70-75,41-79", "40-48,47-92", "37-41,52-89", "3-3,4-91", "27-77,27-77",
 "64-90,33-52", "28-40,9-64", "2-73,3-95", "17-94,7-75", "6-71,7-71", ...]

Result: 595
:ok
ExUnit.start(autorun: false)

defmodule PartOneTest do
  use ExUnit.Case, async: true
  import PartOne

  test "part one" do
    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"
    result = run(input)
    assert result == 2
  end
end

ExUnit.run()
[projects/elixir/aoc/2022/04.livemd#cell:5mej7fxoqmm4f4vdpvlu3cm3i6pchczx:23: PartOne.run/1]
input #=> "2-4,6-8\n2-3,4-5\n5-7,7-9\n2-8,3-7\n6-6,4-6\n2-6,4-8"
|> String.split("\n", trim: true) #=> ["2-4,6-8", "2-3,4-5", "5-7,7-9", "2-8,3-7", "6-6,4-6", "2-6,4-8"]

.
Finished in 0.00 seconds (0.00s async, 0.00s sync)
1 test, 0 failures

Randomized with seed 375839
%{excluded: 0, failures: 0, skipped: 0, total: 1}

Part 2

defmodule Assignment do
  defp in_range(first, number) do
    Enum.at(first, 0) <= number and Enum.at(first, 1) >= number
  end

  def intersect?(first, second) do
    cond do
      in_range(first, Enum.at(second, 0)) or in_range(first, Enum.at(second, 1)) -> true
      in_range(second, Enum.at(first, 0)) or in_range(second, Enum.at(first, 1)) -> true
      true -> false
    end
  end
end

defmodule PartTwo do
  def resolve(input) do
    IO.puts("--- Part One ---")
    IO.puts("Result: #{run(input)}")
  end

  def run(input) do
    input
    |> String.split("\n", trim: true)
    |> Enum.map(fn x ->
      String.split(x, ",", trim: true)
      |> Enum.map(fn x -> String.split(x, "-", trim: true) end)
      |> Enum.map(fn x ->
        Enum.map(x, fn x ->
          String.to_integer(x)
        end)
      end)
    end)
    |> Enum.count(fn x ->
      Assignment.intersect?(Enum.at(x, 0), Enum.at(x, 1))
    end)
  end
end
{:module, PartTwo, <<70, 79, 82, 49, 0, 0, 12, ...>>, {:run, 1}}
input
|> PartTwo.resolve()
--- Part One ---
Result: 952
:ok
ExUnit.start(autorun: false)

defmodule PartTwoTest do
  use ExUnit.Case, async: true
  import PartTwo

  test "part two" do
    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"
    result = run(input)
    assert result == 4
  end
end

ExUnit.run()
.
Finished in 0.00 seconds (0.00s async, 0.00s sync)
1 test, 0 failures

Randomized with seed 375839
%{excluded: 0, failures: 0, skipped: 0, total: 1}