Day 05
Mix.install([:kino_aoc])
Section
{:ok, puzzle_input} =
KinoAOC.download_puzzle("2024", "5", System.fetch_env!("LB_ADVENT_OF_CODE_SESSION"))
{:ok,
"75|21\n28|29\n28|73\n95|48\n95|19\n95|16\n81|51\n81|95\n81|16\n81|56\n48|59\n48|83\n48|64\n48|57\n48|86\n15|43\n15|82\n15|49\n15|31\n15|35\n15|48\n83|68\n83|26\n83|56\n83|63\n83|64\n83|69\n83|84\n47|83\n47|29\n47|23\n47|18\n47|87\n47|53\n47|79\n47|59\n35|43\n35|34\n35|85\n35|28\n35|64\n35|94\n35|48\n35|79\n35|78\n34|65\n34|88\n34|87\n34|82\n34|45\n34|76\n34|47\n34|39\n34|49\n34|85\n92|15\n92|35\n92|59\n92|69\n92|34\n92|67\n92|26\n92|18\n92|73\n92|84\n92|19\n94|47\n94|83\n94|82\n94|85\n94|57\n94|48\n94|64\n94|28\n94|43\n94|76\n94|49\n94|88\n29|59\n29|51\n29|15\n29|73\n29|89\n29|66\n29|76\n29|75\n29|63\n29|53\n29|18\n29|62\n29|95\n69|94\n69|56\n69|66\n69|43\n69|85\n69|63\n69|82\n69|57\n69|78\n69|15\n69|34\n69|31\n69|35\n69|16\n63|78\n63|65\n63|43\n63|27\n63|66\n63|23\n63|62\n63|35\n63|34\n63|48\n63|56\n63|19\n63|94\n63|85\n63|57\n31|92\n31|28\n31|53\n31|49\n31|64\n31|59\n31|26\n31|47\n31|23\n31|76\n31|89\n31|79\n31|81\n31|83\n31|86\n31|45\n56|86\n56|43\n56|65\n56|57\n56|82\n56|47\n56|85\n56|48\n56|62\n56|31\n56|78\n56|35\n56|16\n56|87\n56|23\n56|95\n56|34\n23|76\n23|29\n23|39\n23|53\n23|45\n23|26\n23|27\n23|28\n23|79\n23|21\n23|84\n23|83\n23|73\n23|86\n23|81\n23|59\n23|68\n23|89\n65|53\n65|48\n65|87\n65|88\n65|27\n65|28\n65|49\n65|45\n65|82\n65|43\n65|29\n65|39\n65|76\n65|85\n65|64\n65|23\n65|31\n65|79\n65|92\n45|68\n45|59\n45|69\n45|73\n45|53\n45|21\n45|81\n45|62\n45|56\n45|15\n45|95\n45|35\n45|63\n45|51\n45|26\n45|19\n45|92\n45|66\n45|89\n45|78\n76|73\n76|51\n76|18\n76|75\n76|95\n76|66\n76|56\n76|69\n76|81\n76|21\n76|67\n76|26\n76|53\n76|92\n76|89\n76|35\n76|63\n76|19\n76|62\n76|59\n76|45\n53|92\n53|21\n53|18\n53|94\n53|67\n53|68\n53|69\n53|63\n53|66\n53|84\n53|34\n53|59\n53|89\n53|73\n53|78\n53|81\n53|19\n53|35\n53|26\n53|56\n53|15\n53|51\n51|47\n51|15\n51|69\n51|65\n51|94\n51|66\n51|31\n51|95\n51|43\n51|35\n51|78\n51|82\n51|85\n51|48\n51|23\n51|63\n51|34\n51|16\n51|88\n51|67\n51|62\n51|19\n51|56\n79|84\n79|28\n79|69\n79|45\n79|92\n79|83\n79|29\n79|53\n79|64\n79|76\n79|89\n79|51\n79|68\n79|39\n79|26\n79|81\n79|21\n79|63\n79|62\n79|59\n79|75\n79|56\n79|73\n79|18\n19|35\n19|48\n19|82\n19|94\n19|57\n19|78\n19|43\n19|79\n19|83\n19|31\n19|64\n19|23\n19|47\n19|67\n19|28\n19|85\n19|27\n19|86\n19|34\n19|65\n19|87\n19|88\n19|16\n19|49\n82|27\n82|68\n82|83\n82|39\n82|28\n82|73\n82|92\n82|49\n82|75\n82|89\n82|31\n82|45\n82|47\n82|18\n82|87\n82|53\n82|79\n82|23\n82|86\n82|81\n82|29\n82|59\n82|64\n82|76\n21|34\n21|19\n21|78\n21|57\n21|82\n21|51\n21|66\n21|35\n21|95\n21|62\n21|48\n21|84\n21|65\n21|69\n21|31\n21|56\n21|15\n21|67\n21|85\n21|16\n21|88\n21|94\n21|63\n21|43\n88|27\n88|29\n88|64\n88|75\n88|31\n88|59\n88|76\n88|39\n88|47\n88|28\n88|87\n88|23\n88|83\n88|81\n88|53\n88|86\n88|45\n88|18\n88|92\n88|79\n88|49\n88|57\n88|82\n88|89\n86|18\n86|28\n86|73\n86|75\n86|59\n86|26\n86|21\n86|69\n86|76\n86|79\n86|51\n86|92\n86|45\n86|83\n86|64\n86|49\n86|81\n86|68\n86|39\n86|53\n86|29\n86|84\n86|63\n86|89\n43|59\n43|64\n43|87\n43|47\n43|49\n43|57\n43|31\n43|86\n43|39\n43|27\n43|53\n43|88\n43|83\n43|89\n43|81\n43|79\n43|75\n43|82\n43|23\n43|76\n43|29\n43|28\n43|92\n43|45\n26|15\n26|82\n26|78\n26|67\n26|57\n26|88\n26|35\n26|43\n26|85\n26|21\n26|48\n26|84\n26|69\n26|62\n26|34\n26|19\n26|66\n26|65\n26|51\n26|16\n26|95\n26|94\n26|63\n26|56\n68|85\n68|65\n68|95\n68|35\n68|78\n68|67\n68|57\n68|51\n68|62\n68|56\n68|34\n68|21\n68|63\n68|88\n68|15\n68|48\n68|43\n68|26\n68|66\n68|84\n68|94\n68|19\n68|69\n68|16\n85|64\n85|27\n85|82\n85|43\n85|53\n85|48\n85|57\n85|86\n85|88\n85|29\n85|47\n85|87\n85|83\n85|75\n85|76\n85|28\n85|81\n85|49\n85|45\n85|39\n85|92\n85|31\n85|23\n85|79\n39|84\n39|76\n39|59\n39|67\n39|62\n39|92\n39|81\n39|69\n39|66\n39|26\n39|15\n39|75\n39|95\n39|63\n39|21\n39|73\n39|45\n39|51\n39|89\n39|53\n39|56\n39|68\n39|19\n39|18\n78|48\n78|87\n78|34\n78|29\n78|28\n78|79\n78|76\n78|43\n78|39\n78|85\n78|88\n78|49\n78|27\n78|86\n78|94\n78|64\n78|31\n78|83\n78|82\n78|16\n78|23\n78|47\n78|65\n78|57\n87|83\n87|26\n87|51\n87|29\n87|79\n87|45\n87|68\n87|75\n87|73\n87|21\n87|53\n87|81\n87|84\n87|39\n87|89\n87|92\n87|76\n87|59\n87|18\n87|86\n87|69\n87|49\n87|64\n87|28\n18|69\n18|95\n18|73\n18|16\n18|48\n18|21\n18|68\n18|85\n18|62\n18|34\n18|19\n18|65\n18|15\n18|94\n18|63\n18|51\n18|66\n18|84\n18|67\n18|43\n18|35\n18|26\n18|56\n18|78\n84|95\n84|62\n84|48\n84|35\n84|57\n84|31\n84|16\n84|78\n84|65\n84|88\n84|67\n84|34\n84|19\n84|43\n84|69\n84|15\n84|94\n84|63\n84|51\n84|66\n84|47\n84|56\n84|85\n84|82\n59|34\n59|69\n59|89\n59|19\n59|62\n59|84\n59|68\n59|73\n59|26\n59|66\n59|94\n59|67\n59|21\n59|85\n59|18\n59|95\n59|16\n59|15\n59|63\n59|56\n59|35\n59|51\n59|78\n59|65\n67|88\n67|27\n67|16\n67|28\n67|79\n67|64\n67|65\n67|29\n67|47\n67|83\n67|34\n67|48\n67|49\n67|43\n67|94\n67|23\n67|85\n67|35\n67|31\n67|82\n67|57\n67|87\n67|8" <> ...}
[orderings, jobs] = String.split(puzzle_input, "\n\n", parts: 2)
["75|21\n28|29\n28|73\n95|48\n95|19\n95|16\n81|51\n81|95\n81|16\n81|56\n48|59\n48|83\n48|64\n48|57\n48|86\n15|43\n15|82\n15|49\n15|31\n15|35\n15|48\n83|68\n83|26\n83|56\n83|63\n83|64\n83|69\n83|84\n47|83\n47|29\n47|23\n47|18\n47|87\n47|53\n47|79\n47|59\n35|43\n35|34\n35|85\n35|28\n35|64\n35|94\n35|48\n35|79\n35|78\n34|65\n34|88\n34|87\n34|82\n34|45\n34|76\n34|47\n34|39\n34|49\n34|85\n92|15\n92|35\n92|59\n92|69\n92|34\n92|67\n92|26\n92|18\n92|73\n92|84\n92|19\n94|47\n94|83\n94|82\n94|85\n94|57\n94|48\n94|64\n94|28\n94|43\n94|76\n94|49\n94|88\n29|59\n29|51\n29|15\n29|73\n29|89\n29|66\n29|76\n29|75\n29|63\n29|53\n29|18\n29|62\n29|95\n69|94\n69|56\n69|66\n69|43\n69|85\n69|63\n69|82\n69|57\n69|78\n69|15\n69|34\n69|31\n69|35\n69|16\n63|78\n63|65\n63|43\n63|27\n63|66\n63|23\n63|62\n63|35\n63|34\n63|48\n63|56\n63|19\n63|94\n63|85\n63|57\n31|92\n31|28\n31|53\n31|49\n31|64\n31|59\n31|26\n31|47\n31|23\n31|76\n31|89\n31|79\n31|81\n31|83\n31|86\n31|45\n56|86\n56|43\n56|65\n56|57\n56|82\n56|47\n56|85\n56|48\n56|62\n56|31\n56|78\n56|35\n56|16\n56|87\n56|23\n56|95\n56|34\n23|76\n23|29\n23|39\n23|53\n23|45\n23|26\n23|27\n23|28\n23|79\n23|21\n23|84\n23|83\n23|73\n23|86\n23|81\n23|59\n23|68\n23|89\n65|53\n65|48\n65|87\n65|88\n65|27\n65|28\n65|49\n65|45\n65|82\n65|43\n65|29\n65|39\n65|76\n65|85\n65|64\n65|23\n65|31\n65|79\n65|92\n45|68\n45|59\n45|69\n45|73\n45|53\n45|21\n45|81\n45|62\n45|56\n45|15\n45|95\n45|35\n45|63\n45|51\n45|26\n45|19\n45|92\n45|66\n45|89\n45|78\n76|73\n76|51\n76|18\n76|75\n76|95\n76|66\n76|56\n76|69\n76|81\n76|21\n76|67\n76|26\n76|53\n76|92\n76|89\n76|35\n76|63\n76|19\n76|62\n76|59\n76|45\n53|92\n53|21\n53|18\n53|94\n53|67\n53|68\n53|69\n53|63\n53|66\n53|84\n53|34\n53|59\n53|89\n53|73\n53|78\n53|81\n53|19\n53|35\n53|26\n53|56\n53|15\n53|51\n51|47\n51|15\n51|69\n51|65\n51|94\n51|66\n51|31\n51|95\n51|43\n51|35\n51|78\n51|82\n51|85\n51|48\n51|23\n51|63\n51|34\n51|16\n51|88\n51|67\n51|62\n51|19\n51|56\n79|84\n79|28\n79|69\n79|45\n79|92\n79|83\n79|29\n79|53\n79|64\n79|76\n79|89\n79|51\n79|68\n79|39\n79|26\n79|81\n79|21\n79|63\n79|62\n79|59\n79|75\n79|56\n79|73\n79|18\n19|35\n19|48\n19|82\n19|94\n19|57\n19|78\n19|43\n19|79\n19|83\n19|31\n19|64\n19|23\n19|47\n19|67\n19|28\n19|85\n19|27\n19|86\n19|34\n19|65\n19|87\n19|88\n19|16\n19|49\n82|27\n82|68\n82|83\n82|39\n82|28\n82|73\n82|92\n82|49\n82|75\n82|89\n82|31\n82|45\n82|47\n82|18\n82|87\n82|53\n82|79\n82|23\n82|86\n82|81\n82|29\n82|59\n82|64\n82|76\n21|34\n21|19\n21|78\n21|57\n21|82\n21|51\n21|66\n21|35\n21|95\n21|62\n21|48\n21|84\n21|65\n21|69\n21|31\n21|56\n21|15\n21|67\n21|85\n21|16\n21|88\n21|94\n21|63\n21|43\n88|27\n88|29\n88|64\n88|75\n88|31\n88|59\n88|76\n88|39\n88|47\n88|28\n88|87\n88|23\n88|83\n88|81\n88|53\n88|86\n88|45\n88|18\n88|92\n88|79\n88|49\n88|57\n88|82\n88|89\n86|18\n86|28\n86|73\n86|75\n86|59\n86|26\n86|21\n86|69\n86|76\n86|79\n86|51\n86|92\n86|45\n86|83\n86|64\n86|49\n86|81\n86|68\n86|39\n86|53\n86|29\n86|84\n86|63\n86|89\n43|59\n43|64\n43|87\n43|47\n43|49\n43|57\n43|31\n43|86\n43|39\n43|27\n43|53\n43|88\n43|83\n43|89\n43|81\n43|79\n43|75\n43|82\n43|23\n43|76\n43|29\n43|28\n43|92\n43|45\n26|15\n26|82\n26|78\n26|67\n26|57\n26|88\n26|35\n26|43\n26|85\n26|21\n26|48\n26|84\n26|69\n26|62\n26|34\n26|19\n26|66\n26|65\n26|51\n26|16\n26|95\n26|94\n26|63\n26|56\n68|85\n68|65\n68|95\n68|35\n68|78\n68|67\n68|57\n68|51\n68|62\n68|56\n68|34\n68|21\n68|63\n68|88\n68|15\n68|48\n68|43\n68|26\n68|66\n68|84\n68|94\n68|19\n68|69\n68|16\n85|64\n85|27\n85|82\n85|43\n85|53\n85|48\n85|57\n85|86\n85|88\n85|29\n85|47\n85|87\n85|83\n85|75\n85|76\n85|28\n85|81\n85|49\n85|45\n85|39\n85|92\n85|31\n85|23\n85|79\n39|84\n39|76\n39|59\n39|67\n39|62\n39|92\n39|81\n39|69\n39|66\n39|26\n39|15\n39|75\n39|95\n39|63\n39|21\n39|73\n39|45\n39|51\n39|89\n39|53\n39|56\n39|68\n39|19\n39|18\n78|48\n78|87\n78|34\n78|29\n78|28\n78|79\n78|76\n78|43\n78|39\n78|85\n78|88\n78|49\n78|27\n78|86\n78|94\n78|64\n78|31\n78|83\n78|82\n78|16\n78|23\n78|47\n78|65\n78|57\n87|83\n87|26\n87|51\n87|29\n87|79\n87|45\n87|68\n87|75\n87|73\n87|21\n87|53\n87|81\n87|84\n87|39\n87|89\n87|92\n87|76\n87|59\n87|18\n87|86\n87|69\n87|49\n87|64\n87|28\n18|69\n18|95\n18|73\n18|16\n18|48\n18|21\n18|68\n18|85\n18|62\n18|34\n18|19\n18|65\n18|15\n18|94\n18|63\n18|51\n18|66\n18|84\n18|67\n18|43\n18|35\n18|26\n18|56\n18|78\n84|95\n84|62\n84|48\n84|35\n84|57\n84|31\n84|16\n84|78\n84|65\n84|88\n84|67\n84|34\n84|19\n84|43\n84|69\n84|15\n84|94\n84|63\n84|51\n84|66\n84|47\n84|56\n84|85\n84|82\n59|34\n59|69\n59|89\n59|19\n59|62\n59|84\n59|68\n59|73\n59|26\n59|66\n59|94\n59|67\n59|21\n59|85\n59|18\n59|95\n59|16\n59|15\n59|63\n59|56\n59|35\n59|51\n59|78\n59|65\n67|88\n67|27\n67|16\n67|28\n67|79\n67|64\n67|65\n67|29\n67|47\n67|83\n67|34\n67|48\n67|49\n67|43\n67|94\n67|23\n67|85\n67|35\n67|31\n67|82\n67|57\n67|87\n67|8" <> ...,
"94,68,73,78,15,65,43,66,62\n34,65,85,48,82,47,23,87,49,79,83,28,29\n57,82,27,87,83,28,29,39,76,45,75,53,92,81,18\n29,86,68,39,81,45,75,76,31,53,64,23,92\n31,28,83,49,87,79,82,64,16,48,86\n45,89,73,53,59,68,28,83,86,79,69,76,39\n31,19,88,35,23,43,86,79,48,16,57,82,27,87,67,65,78,34,49,47,66\n31,47,23,27,87,49,79,83,28,64,29,76,45,75,53,81,59,89,18,73,68\n26,95,73,56,81,89,59,34,92,94,21\n16,63,34,19,66,65,47,56,31,82,51,85,57,67,88,62,48,78,95,35,15\n62,15,66,95,19,67,35,78,94,16,65,48,43,88,82,31,47,23,27,87,86\n62,15,66,95,67,35,34,16,65,43,88,82,31,47,86\n28,68,83,31,59,89,29,73,39,75,92,86,76,45,49,64,18,81,87,27,79,23,47\n81,78,26,16,59,62,51,19,21,56,68,94,18,73,69,35,89,84,66\n83,76,75,53,92,81,73,26,63,56,62\n53,27,29,79,28,88,85,31,47\n29,75,43,31,83,57,49,27,48,64,23,87,86,76,79,53,45\n63,27,43,95,78,56,34\n83,59,89,81,18,84,68,76,69,26,62,63,56,75,73,92,64,53,28,51,29\n75,53,92,18,73,68,26,51,69,63,15,19,67,35,78\n48,31,23,27,87,86,28,64,29,76,75\n16,51,62,43,15,68,78,65,88,19,34,85,26,48,95,94,67,56,84,69,66\n65,82,23,87,86,75,48,49,31,16,57,45,43,47,29\n29,76,45,75,81,18,73,21,56,62,95\n18,67,21,15,63,59,84,66,69,26,34,68,35,95,81,92,19\n49,79,39,76,53,92,59,89,18,68,51,69,63\n85,15,43,62,84,88,57,16,31,95,69\n21,51,43,16,94,78,19\n87,86,49,79,83,28,39,76,45,53,92,89,18,68,26,21,51\n51,69,62,35,88,15,16,21,48,57,26,78,65,43,34,95,19\n49,78,16,94,31,23,65\n87,83,29,45,59,73,51\n34,94,16,85,48,43,88,57,82,23,87,86,79,83,28,29,76\n84,51,69,63,56,62,15,66,95,19,67,35,78,34,94,85,43,88,57,82,31\n53,69,49,84,51,89,28,63,29\n83,21,64,68,75,59,87,45,76,23,29,73,18,27,28,81,26,53,86,89,39,92,79\n62,15,67,35,16,43,86\n88,31,23,83,39\n27,87,86,49,83,64,39,76,75,53,89,73,26,21,84\n53,81,21,51,63,15,95,35,34\n95,51,31,84,15,94,43\n92,76,59,63,73,64,84,81,28,75,15,29,68,62,69\n89,18,62,15,94,65,85\n92,68,63,95,67,78,94\n94,68,95,84,16,63,15,35,26,34,62,73,19,51,56,67,18,21,69,65,66,59,78\n35,34,16,65,48,27,86,83,29\n15,66,95,67,34,94,85,48,43,82,23\n69,63,56,66,95,19,67,35,78,34,94,16,65,85,48,43,23\n85,27,66,19,43,62,86\n65,85,48,43,82,31,47,49,79,28,39,76,45,75,53\n86,49,79,64,39,76,45,75,59,89,84,51,69\n92,81,59,89,18,73,26,21,84,51,69,63,56,62,15,66,95,19,67,35,78,34,94\n26,84,69,63,56,62,66,19,67,78,34,94,16,85,43,88,57\n21,89,19,84,94,35,68,65,18,51,16,63,59,67,34,56,73,78,15,69,95,66,26\n83,23,39,27,76,47,49,29,48,86,82,53,88,64,92,75,81,45,31,43,57\n34,94,16,65,48,57,82,31,23,27,83,28,76\n95,51,62,63,16,34,65,66,69,19,57,82,47\n82,57,84,65,78,31,16,67,66\n64,39,92,81,89,69,66\n59,95,18,68,15,34,35,51,19,92,66,56,94,81,73,62,78\n83,28,64,29,39,76,45,75,53,92,81,89,73,68,26,21,51,69,63,56,62\n66,73,39,64,56,76,59\n68,26,69,56,85,43,88\n67,35,78,94,16,65,48,88,57,82,47,23,49,79,83,28,64\n92,81,26,51,69,63,62,15,95,19,78,34,94\n81,59,73,68,26,51,63,56,15,95,35,34,16\n79,83,64,45,53,81,68,26,84,51,69,63,56\n89,53,84,39,62,63,51,75,64,81,26,18,69,83,45,29,56\n82,87,39,88,23,57,48,85,76,83,92,47,43\n48,43,57,27,87,79,83,76,45,75,53,92,81\n82,81,75,39,88\n43,88,57,82,31,47,23,27,87,86,49,79,83,28,64,29,39,76,45,75,53,81,59\n88,87,86,49,79,83,28,39,76,45,92,81,89\n59,76,29,23,89,79,31,73,45,82,87,92,86\n66,95,19,67,35,78,34,94,16,85,48,43,88,57,82,31,47,23,27,87,86,49,79\n23,27,86,79,83,28,64,29,39,76,45,92,59,89,73,68,21\n23,31,39,53,89,64,79,73,59,45,92\n63,85,66,35,65,15,31,62,95\n35,88,31,78,23,87,15,49,95,27,57\n79,83,28,64,29,76,45,75,53,92,81,59,89,68,26,21,51,69,63\n43,16,73,34,19,66,62\n84,51,56,62,95,67,78,34,85,48,57,82,31\n76,45,75,53,92,81,59,18,73,26,21,51,69,63,56,15,66,95,19\n49,79,83,28,64,29,39,45,75,53,92,81,59,89,18,73,68,26,21,84,51,69,63\n63,51,34,19,15,65,31,57,48,95,88,66,56,67,62,16,35,94,78,84,43\n45,48,83,43,65,29,94,82,76,23,39\n83,49,47,45,88,64,85,28,27\n31,19,35,16,49,88,65,43,48,83,94,67,28\n84,65,88,51,85,43,78,19,21,69,48,66,15,35,68\n21,15,66,19,35,78,94,16,65,88,57\n19,67,78,34,94,65,85,48,43,88,57,47,23,27,87,86,49,83,28\n67,82,87,48,23,65,43,19,95,56,66,35,16\n88,82,31,47,23,27,87,86,49,79,83,28,64,29,39,76,45,53,81,59,89\n92,81,59,73,68,26,21,84,51,69,63,56,15,66,95,19,67,35,78,34,94\n79,76,29,21,68,86,83,53,59,26,49,89,27,87,45,28,81\n49,79,83,28,64,29,39,76,45,75,53,92,81,59,8" <> ...]
orderings =
orderings
|> String.split("\n", trim: true)
|> Enum.map(fn row ->
row
|> String.split("|")
|> Enum.map(&String.to_integer/1)
|> List.to_tuple()
end)
prev =
orderings
|> Enum.group_by(&elem(&1, 1), &elem(&1, 0))
%{
73 => [28, 92, 29, 23, 45, 76, 53, 79, 82, 86, 39, 87, 18, 59, 57, 64, 89, 49, 27, 75, 81, 83, 47,
31],
23 => [47, 63, 31, 56, 65, 51, 19, 82, 88, 43, 85, 78, 67, 57, 16, 62, 66, 95, 48, 15, 35, 34, 94,
69],
29 => [28, 47, 23, 65, 79, 82, 88, 86, 43, 85, 78, 87, 67, 57, 64, 16, 49, 27, 48, 83, 35, 34, 94,
31],
47 => [34, 94, 31, 56, 51, 19, 82, 88, 43, 85, 78, 84, 67, 57, 16, 62, 66, 95, 48, 15, 35, 69, 63,
65],
89 => [29, 31, 23, 45, 76, 53, 79, 82, 88, 86, 43, 39, 87, 59, 57, 64, 49, 27, 75, 28, 81, 83, 47,
92],
43 => [15, 35, 94, 69, 63, 56, 65, 51, 19, 21, 26, 68, 85, 78, 18, 84, 67, 73, 16, 62, 66, 95, 48,
34],
39 => [34, 23, 65, 79, 82, 88, 86, 43, 85, 78, 87, 57, 64, 16, 49, 27, 28, 48, 83, 47, 35, 94, 29,
31],
45 => [34, 31, 23, 65, 76, 79, 82, 88, 86, 43, 85, 39, 87, 57, 64, 16, 49, 27, 28, 48, 83, 47, 94,
29],
48 => [95, 15, 35, 94, 63, 56, 65, 51, 19, 21, 26, 68, 85, 78, 18, 84, 67, 73, 16, 89, 62, 66, 34,
69],
57 => [48, 94, 69, 63, 56, 19, 21, 88, 43, 26, 68, 85, 78, 84, 67, 16, 62, 66, 95, 15, 35, 34, 65,
51],
26 => [83, 92, 31, 23, 45, 76, 53, 79, 86, 68, 39, 87, 18, 59, 73, 64, 89, 49, 27, 75, 28, 81, 47,
29],
69 => [83, 92, 45, 76, 53, 51, 79, 21, 86, 26, 68, 39, 87, 18, 84, 59, 73, 64, 89, 49, 75, 28, 81,
29],
88 => [34, 94, 65, 51, 19, 21, 43, 26, 68, 85, 78, 84, 67, 73, 16, 62, 66, 95, 48, 15, 35, 69, 63,
56],
63 => [83, 29, 69, 45, 76, 53, 51, 79, 21, 86, 26, 68, 39, 18, 84, 59, 73, 64, 89, 49, 75, 28, 81,
92],
31 => [15, 69, 56, 65, 51, 19, 82, 21, 88, 43, 85, 78, 84, 67, 57, 16, 62, 66, 95, 48, 35, 34, 94,
63],
81 => [31, 23, 45, 76, 53, 79, 82, 88, 86, 43, 85, 39, 87, 57, 64, 49, 27, 75, 28, 48, 83, 47, 92,
29],
76 => [34, 94, 29, 31, 23, 65, 79, 82, 88, 86, 43, 85, 39, 78, 87, 57, 64, 16, 49, 27, 28, 48, 83,
47],
34 => [35, 92, 69, 63, 56, 53, 51, 19, 21, 26, 68, 78, 18, 84, 59, 67, 73, 89, 62, 66, 75, 95, 81,
15],
28 => [35, 94, 31, 23, 65, 79, 19, 82, 88, 86, 43, 85, 78, 87, 67, 57, 16, 49, 27, 95, 48, 83, 47,
34],
85 => [35, 34, 94, 69, 63, 56, 65, 51, 19, 21, 26, 68, 78, 18, 84, 59, 67, 73, 16, 89, 62, 66, 95,
15],
64 => [48, 83, 35, 94, 31, 65, 79, 19, 82, 88, 86, 43, 85, 78, 87, 67, 57, 16, 49, 27, 28, 47, 34,
23],
68 => [83, 23, 45, 53, 79, 82, 86, 39, 87, 18, 59, 73, 64, 89, 49, 27, 75, 28, 81, 47, 92, 29, 31,
76],
86 => [48, 31, 56, 23, 19, 82, 88, 43, 85, 78, 87, 67, 57, 16, 27, 62, 66, 95, 15, 47, 35, 34, 94,
65],
51 => [81, 29, 45, 76, 53, 79, 21, 86, 26, 68, 39, 87, 18, 84, 59, 73, 64, 89, 49, 27, 75, 28, 83,
92],
59 => [48, 47, 92, 29, 31, 23, 45, 76, 53, 79, 82, 88, 86, 43, 39, 87, 57, 64, 49, 27, 75, 28, 81,
83],
95 => [81, 29, 56, 45, 76, 51, 21, 26, 68, 39, 18, 84, 59, 73, 64, 89, 62, 66, 75, 15, 92, 69, 63,
53],
78 => [35, 69, 63, 56, 45, 53, 51, 19, 21, 26, 68, 18, 84, 59, 67, 73, 89, 62, 66, 75, 95, 81, 15,
...],
15 => [92, 29, 69, 45, 53, 51, 21, 26, 68, 39, 18, 84, 59, 73, 64, 89, 62, 75, 28, 81, 83, 63,
...],
83 => [48, 47, 94, 31, 23, 79, 19, 82, 88, 86, 43, 85, 78, 87, 67, 57, 16, 49, 27, 66, 95, ...],
75 => [29, 76, 79, 82, 88, 86, 43, 85, 39, 87, 57, 64, 16, 49, 27, 28, 48, 83, 47, 94, ...],
87 => [47, 34, 56, 65, 19, 82, 88, 43, 85, 78, 67, 57, 16, 27, 62, 66, 95, 48, 15, ...],
21 => [75, 23, 45, 76, 53, 79, 86, 26, 68, 39, 87, 18, 59, 73, 64, 89, 49, 27, ...],
82 => [15, 34, 94, 69, 56, 65, 51, 19, 21, 88, 43, 26, 85, 78, 84, 67, 57, ...],
53 => [47, 29, 31, 23, 65, 45, 76, 79, 82, 88, 86, 43, 85, 39, 87, 57, ...],
66 => [29, 69, 63, 45, 76, 53, 51, 21, 26, 68, 39, 18, 84, 59, 73, ...],
35 => [15, 92, 69, 63, 56, 45, 76, 53, 51, 19, 21, 26, 68, 18, ...],
56 => [81, 83, 69, 63, 45, 76, 53, 51, 79, 21, 26, 68, 39, ...],
62 => [29, 63, 56, 45, 76, 51, 79, 21, 26, 68, 39, 18, ...],
49 => [15, 34, 94, 31, 65, 19, 82, 88, 86, 43, 85, ...],
27 => [63, 23, 65, 19, 82, 88, 43, 85, 78, 67, ...],
84 => [83, 92, 23, 53, 79, 21, 86, 26, 68, ...],
94 => [35, 69, 63, 53, 51, 19, 21, 26, ...],
92 => [31, 65, 45, 76, 53, 79, 82, ...],
19 => [95, 92, 63, 45, 76, 53, ...],
67 => [92, 76, 53, 51, 19, ...],
65 => [34, 63, 56, 51, ...],
18 => [47, 92, 29, ...],
79 => [47, 35, ...],
16 => [95, ...]
}
jobs =
jobs
|> String.split("\n", trim: true)
|> Enum.map(fn row ->
row
|> String.split(",")
|> Enum.map(&String.to_integer/1)
end)
[
[94, 68, 73, 78, 15, 65, 43, 66, 62],
[34, 65, 85, 48, 82, 47, 23, 87, 49, 79, 83, 28, 29],
[57, 82, 27, 87, 83, 28, 29, 39, 76, 45, 75, 53, 92, 81, 18],
[29, 86, 68, 39, 81, 45, 75, 76, 31, 53, 64, 23, 92],
[31, 28, 83, 49, 87, 79, 82, 64, 16, 48, 86],
[45, 89, 73, 53, 59, 68, 28, 83, 86, 79, 69, 76, 39],
[31, 19, 88, 35, 23, 43, 86, 79, 48, 16, 57, 82, 27, 87, 67, 65, 78, 34, 49, 47, 66],
[31, 47, 23, 27, 87, 49, 79, 83, 28, 64, 29, 76, 45, 75, 53, 81, 59, 89, 18, 73, 68],
[26, 95, 73, 56, 81, 89, 59, 34, 92, 94, 21],
[16, 63, 34, 19, 66, 65, 47, 56, 31, 82, 51, 85, 57, 67, 88, 62, 48, 78, 95, 35, 15],
[62, 15, 66, 95, 19, 67, 35, 78, 94, 16, 65, 48, 43, 88, 82, 31, 47, 23, 27, 87, 86],
[62, 15, 66, 95, 67, 35, 34, 16, 65, 43, 88, 82, 31, 47, 86],
[28, 68, 83, 31, 59, 89, 29, 73, 39, 75, 92, 86, 76, 45, 49, 64, 18, 81, 87, 27, 79, 23, 47],
[81, 78, 26, 16, 59, 62, 51, 19, 21, 56, 68, 94, 18, 73, 69, 35, 89, 84, 66],
[83, 76, 75, 53, 92, 81, 73, 26, 63, 56, 62],
[53, 27, 29, 79, 28, 88, 85, 31, 47],
[29, 75, 43, 31, 83, 57, 49, 27, 48, 64, 23, 87, 86, 76, 79, 53, 45],
~c"?\e+_N8\"",
[83, 59, 89, 81, 18, 84, 68, 76, 69, 26, 62, 63, 56, 75, 73, 92, 64, 53, 28, 51, 29],
[75, 53, 92, 18, 73, 68, 26, 51, 69, 63, 15, 19, 67, 35, 78],
[48, 31, 23, 27, 87, 86, 28, 64, 29, 76, 75],
[16, 51, 62, 43, 15, 68, 78, 65, 88, 19, 34, 85, 26, 48, 95, 94, 67, 56, 84, 69, 66],
[65, 82, 23, 87, 86, 75, 48, 49, 31, 16, 57, 45, 43, 47, 29],
[29, 76, 45, 75, 81, 18, 73, 21, 56, 62, 95],
[18, 67, 21, 15, 63, 59, 84, 66, 69, 26, 34, 68, 35, 95, 81, 92, 19],
[49, 79, 39, 76, 53, 92, 59, 89, 18, 68, 51, 69, 63],
[85, 15, 43, 62, 84, 88, 57, 16, 31, 95, 69],
[21, 51, 43, 16, 94, 78, 19],
[87, 86, 49, 79, 83, 28, 39, 76, 45, 53, 92, 89, 18, 68, 26, 21, 51],
[51, 69, 62, 35, 88, 15, 16, 21, 48, 57, 26, 78, 65, 43, 34, 95, 19],
[49, 78, 16, 94, 31, 23, 65],
[87, 83, 29, 45, 59, 73, 51],
[34, 94, 16, 85, 48, 43, 88, 57, 82, 23, 87, 86, 79, 83, 28, 29, 76],
[84, 51, 69, 63, 56, 62, 15, 66, 95, 19, 67, 35, 78, 34, 94, 85, ...],
[53, 69, 49, 84, 51, 89, 28, 63, 29],
[83, 21, 64, 68, 75, 59, 87, 45, 76, 23, 29, 73, 18, 27, ...],
[62, 15, 67, 35, 16, 43, 86],
[88, 31, 23, 83, 39],
[27, 87, 86, 49, 83, 64, 39, 76, 75, 53, 89, ...],
[53, 81, 21, 51, 63, 15, 95, 35, 34],
[95, 51, 31, 84, 15, 94, 43],
[92, 76, 59, 63, 73, 64, 84, 81, ...],
[89, 18, 62, 15, 94, 65, 85],
~c"\\D?_CN^",
[94, 68, 95, 84, 16, ...],
[35, 34, 16, 65, ...],
[15, 66, 95, ...],
[69, 63, ...],
[85, ...],
[...],
...
]
defmodule PrintQueue do
def valid?(input, prev), do: do_valid?([], input, prev)
defp do_valid?(_, [], _prev), do: true
defp do_valid?(visited, [a | rest], prev) do
if Enum.any?(prev[a], & &1 in rest) do
false
else
do_valid?([a | visited], rest, prev)
end
end
def middle(list) do
Enum.at(list, div(length(list), 2))
end
end
{:module, PrintQueue, <<70, 79, 82, 49, 0, 0, 10, ...>>, {:middle, 1}}
{valid, invalid} = Enum.split_with(jobs, &PrintQueue.valid?(&1, prev))
{[
[34, 65, 85, 48, 82, 47, 23, 87, 49, 79, 83, 28, 29],
[57, 82, 27, 87, 83, 28, 29, 39, 76, 45, 75, 53, 92, 81, 18],
[31, 47, 23, 27, 87, 49, 79, 83, 28, 64, 29, 76, 45, 75, 53, 81, 59, 89, 18, 73, 68],
[62, 15, 66, 95, 19, 67, 35, 78, 94, 16, 65, 48, 43, 88, 82, 31, 47, 23, 27, 87, 86],
[62, 15, 66, 95, 67, 35, 34, 16, 65, 43, 88, 82, 31, 47, 86],
[83, 76, 75, 53, 92, 81, 73, 26, 63, 56, 62],
[75, 53, 92, 18, 73, 68, 26, 51, 69, 63, 15, 19, 67, 35, 78],
[48, 31, 23, 27, 87, 86, 28, 64, 29, 76, 75],
[29, 76, 45, 75, 81, 18, 73, 21, 56, 62, 95],
[49, 79, 39, 76, 53, 92, 59, 89, 18, 68, 51, 69, 63],
[87, 86, 49, 79, 83, 28, 39, 76, 45, 53, 92, 89, 18, 68, 26, 21, 51],
[87, 83, 29, 45, 59, 73, 51],
[34, 94, 16, 85, 48, 43, 88, 57, 82, 23, 87, 86, 79, 83, 28, 29, 76],
[84, 51, 69, 63, 56, 62, 15, 66, 95, 19, 67, 35, 78, 34, 94, 85, 43, 88, 57, 82, 31],
[62, 15, 67, 35, 16, 43, 86],
[88, 31, 23, 83, 39],
[27, 87, 86, 49, 83, 64, 39, 76, 75, 53, 89, 73, 26, 21, 84],
[53, 81, 21, 51, 63, 15, 95, 35, 34],
[89, 18, 62, 15, 94, 65, 85],
~c"\\D?_CN^",
[35, 34, 16, 65, 48, 27, 86, 83, 29],
[15, 66, 95, 67, 34, 94, 85, 48, 43, 82, 23],
[69, 63, 56, 66, 95, 19, 67, 35, 78, 34, 94, 16, 65, 85, 48, 43, 23],
[65, 85, 48, 43, 82, 31, 47, 49, 79, 28, 39, 76, 45, 75, 53],
~c"V1O@'L-K;YT3E",
[92, 81, 59, 89, 18, 73, 26, 21, 84, 51, 69, 63, 56, 62, 15, 66, 95, 19, 67, 35, 78, 34, 94],
[26, 84, 69, 63, 56, 62, 66, 19, 67, 78, 34, 94, 16, 85, 43, 88, 57],
[34, 94, 16, 65, 48, 57, 82, 31, 23, 27, 83, 28, 76],
~c"@'\\QYEB",
[83, 28, 64, 29, 39, 76, 45, 75, 53, 92, 81, 89, 73, 68, 26, 21, 51, 69, 63, ...],
[68, 26, 69, 56, 85, 43, 88],
[67, 35, 78, 94, 16, 65, 48, 88, 57, 82, 47, 23, 49, 79, 83, 28, 64],
[92, 81, 26, 51, 69, 63, 62, 15, 95, 19, 78, 34, 94],
[81, 59, 73, 68, 26, 51, 63, 56, 15, 95, 35, 34, 16],
[79, 83, 64, 45, 53, 81, 68, 26, 84, 51, 69, 63, 56],
~c"0+9\eWOSL-K5\\Q",
[43, 88, 57, 82, 31, 47, 23, 27, 87, 86, 49, 79, ...],
[88, 87, 86, 49, 79, 83, 28, 39, 76, 45, 92, ...],
[66, 95, 19, 67, 35, 78, 34, 94, 16, 85, ...],
[23, 27, 86, 79, 83, 28, 64, 29, 39, ...],
[79, 83, 28, 64, 29, 76, 45, 75, ...],
[84, 51, 56, 62, 95, 67, 78, ...],
[76, 45, 75, 53, 92, 81, ...],
[49, 79, 83, 28, 64, ...],
[21, 15, 66, 19, ...],
[19, 67, 78, ...],
[88, 82, ...],
[92, ...],
[...],
...
],
[
[94, 68, 73, 78, 15, 65, 43, 66, 62],
[29, 86, 68, 39, 81, 45, 75, 76, 31, 53, 64, 23, 92],
[31, 28, 83, 49, 87, 79, 82, 64, 16, 48, 86],
[45, 89, 73, 53, 59, 68, 28, 83, 86, 79, 69, 76, 39],
[31, 19, 88, 35, 23, 43, 86, 79, 48, 16, 57, 82, 27, 87, 67, 65, 78, 34, 49, 47, 66],
[26, 95, 73, 56, 81, 89, 59, 34, 92, 94, 21],
[16, 63, 34, 19, 66, 65, 47, 56, 31, 82, 51, 85, 57, 67, 88, 62, 48, 78, 95, 35, 15],
[28, 68, 83, 31, 59, 89, 29, 73, 39, 75, 92, 86, 76, 45, 49, 64, 18, 81, 87, 27, 79, 23, 47],
[81, 78, 26, 16, 59, 62, 51, 19, 21, 56, 68, 94, 18, 73, 69, 35, 89, 84, 66],
[53, 27, 29, 79, 28, 88, 85, 31, 47],
[29, 75, 43, 31, 83, 57, 49, 27, 48, 64, 23, 87, 86, 76, 79, 53, 45],
~c"?\e+_N8\"",
[83, 59, 89, 81, 18, 84, 68, 76, 69, 26, 62, 63, 56, 75, 73, 92, 64, 53, 28, 51, 29],
[16, 51, 62, 43, 15, 68, 78, 65, 88, 19, 34, 85, 26, 48, 95, 94, 67, 56, 84, 69, 66],
[65, 82, 23, 87, 86, 75, 48, 49, 31, 16, 57, 45, 43, 47, 29],
[18, 67, 21, 15, 63, 59, 84, 66, 69, 26, 34, 68, 35, 95, 81, 92, 19],
[85, 15, 43, 62, 84, 88, 57, 16, 31, 95, 69],
[21, 51, 43, 16, 94, 78, 19],
[51, 69, 62, 35, 88, 15, 16, 21, 48, 57, 26, 78, 65, 43, 34, 95, 19],
[49, 78, 16, 94, 31, 23, 65],
[53, 69, 49, 84, 51, 89, 28, 63, 29],
[83, 21, 64, 68, 75, 59, 87, 45, 76, 23, 29, 73, 18, 27, 28, 81, 26, 53, 86, 89, 39, 92, 79],
[95, 51, 31, 84, 15, 94, 43],
[92, 76, 59, 63, 73, 64, 84, 81, 28, 75, 15, 29, 68, 62, 69],
[94, 68, 95, 84, 16, 63, 15, 35, 26, 34, 62, 73, 19, 51, 56, 67, 18, 21, 69, 65, 66, 59, 78],
[85, 27, 66, 19, 43, 62, 86],
[21, 89, 19, 84, 94, 35, 68, 65, 18, 51, 16, 63, 59, 67, 34, 56, 73, 78, 15, 69, 95, ...],
[83, 23, 39, 27, 76, 47, 49, 29, 48, 86, 82, 53, 88, 64, 92, 75, 81, 45, 31, 43, ...],
[95, 51, 62, 63, 16, 34, 65, 66, 69, 19, 57, 82, 47],
[82, 57, 84, 65, 78, 31, 16, 67, 66],
[59, 95, 18, 68, 15, 34, 35, 51, 19, 92, 66, 56, 94, 81, 73, 62, 78],
~c"BI'@8L;",
[89, 53, 84, 39, 62, 63, 51, 75, 64, 81, 26, 18, 69, 83, 45, ...],
[82, 87, 39, 88, 23, 57, 48, 85, 76, 83, 92, 47, 43],
~c"RQK'X",
[59, 76, 29, 23, 89, 79, 31, 73, 45, 82, 87, 92, ...],
[23, 31, 39, 53, 89, 64, 79, 73, 59, 45, 92],
[63, 85, 66, 35, 65, 15, 31, 62, 95],
[35, 88, 31, 78, 23, 87, 15, 49, 95, ...],
[43, 16, 73, 34, 19, 66, 62],
[63, 51, 34, 19, 15, 65, 31, ...],
[45, 48, 83, 43, 65, 29, ...],
[83, 49, 47, 45, 88, ...],
[31, 19, 35, 16, ...],
[84, 65, 88, ...],
[67, 82, ...],
[79, ...],
[...],
...
]}
Part 1
valid
|> Enum.map(&PrintQueue.middle/1)
|> Enum.sum()
6051
Part 2
invalid
|> Enum.map(fn job ->
Enum.sort(job, fn a, b -> b in prev[a] end)
end)
|> Enum.map(&PrintQueue.middle/1)
|> Enum.sum()
5093