From 97ff61495e5b7d68c82e238e266ba1a3c5f0a3e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Charv=C3=A1t?= Date: Fri, 6 Dec 2024 01:36:50 +0100 Subject: [PATCH] Day05 There's a recursive, dfs-based topsort implementation here that I'm pretty proud of --- day05/gleam.toml | 20 + day05/input.txt | 1381 +++++++++++++++++++++++++++++++++++ day05/manifest.toml | 14 + day05/small-in.txt | 28 + day05/src/day05.gleam | 110 +++ day05/test/day05_test.gleam | 27 + 6 files changed, 1580 insertions(+) create mode 100644 day05/gleam.toml create mode 100644 day05/input.txt create mode 100644 day05/manifest.toml create mode 100644 day05/small-in.txt create mode 100644 day05/src/day05.gleam create mode 100644 day05/test/day05_test.gleam diff --git a/day05/gleam.toml b/day05/gleam.toml new file mode 100644 index 0000000..1730560 --- /dev/null +++ b/day05/gleam.toml @@ -0,0 +1,20 @@ +name = "day05" +version = "1.0.0" + +# Fill out these fields if you intend to generate HTML documentation or publish +# your project to the Hex package manager. +# +# description = "" +# licences = ["Apache-2.0"] +# repository = { type = "github", user = "", repo = "" } +# links = [{ title = "Website", href = "" }] +# +# For a full reference of all the available options, you can have a look at +# https://gleam.run/writing-gleam/gleam-toml/. + +[dependencies] +gleam_stdlib = ">= 0.34.0 and < 2.0.0" +simplifile = ">= 2.2.0 and < 3.0.0" + +[dev-dependencies] +gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/day05/input.txt b/day05/input.txt new file mode 100644 index 0000000..6b5aa61 --- /dev/null +++ b/day05/input.txt @@ -0,0 +1,1381 @@ +48|39 +26|97 +26|94 +76|66 +76|58 +76|88 +75|61 +75|71 +75|57 +75|74 +69|36 +69|89 +69|65 +69|51 +69|96 +62|72 +62|81 +62|79 +62|76 +62|99 +62|65 +82|62 +82|69 +82|92 +82|57 +82|35 +82|87 +82|89 +28|72 +28|35 +28|79 +28|78 +28|66 +28|26 +28|68 +28|31 +16|61 +16|12 +16|99 +16|46 +16|82 +16|38 +16|51 +16|75 +16|85 +39|38 +39|86 +39|57 +39|74 +39|35 +39|64 +39|89 +39|68 +39|36 +39|46 +36|65 +36|48 +36|42 +36|55 +36|35 +36|76 +36|17 +36|62 +36|87 +36|57 +36|16 +35|94 +35|85 +35|97 +35|76 +35|26 +35|23 +35|48 +35|87 +35|58 +35|65 +35|78 +35|42 +81|86 +81|89 +81|75 +81|93 +81|51 +81|71 +81|17 +81|57 +81|12 +81|41 +81|82 +81|64 +81|39 +88|46 +88|42 +88|97 +88|92 +88|93 +88|38 +88|66 +88|12 +88|16 +88|95 +88|75 +88|69 +88|82 +88|41 +11|65 +11|62 +11|35 +11|76 +11|88 +11|58 +11|26 +11|17 +11|66 +11|94 +11|68 +11|16 +11|42 +11|45 +11|79 +95|57 +95|86 +95|82 +95|39 +95|69 +95|35 +95|17 +95|61 +95|68 +95|46 +95|41 +95|38 +95|74 +95|36 +95|51 +95|11 +85|89 +85|75 +85|51 +85|68 +85|28 +85|95 +85|41 +85|57 +85|12 +85|11 +85|64 +85|74 +85|71 +85|36 +85|38 +85|82 +85|61 +97|81 +97|61 +97|23 +97|85 +97|69 +97|93 +97|64 +97|95 +97|89 +97|46 +97|12 +97|82 +97|36 +97|51 +97|92 +97|74 +97|75 +97|38 +61|17 +61|79 +61|89 +61|86 +61|68 +61|41 +61|46 +61|71 +61|51 +61|12 +61|11 +61|28 +61|93 +61|36 +61|92 +61|65 +61|38 +61|62 +61|82 +79|85 +79|26 +79|48 +79|87 +79|31 +79|66 +79|55 +79|39 +79|94 +79|88 +79|99 +79|95 +79|75 +79|76 +79|97 +79|96 +79|81 +79|45 +79|16 +79|23 +74|11 +74|57 +74|26 +74|79 +74|28 +74|71 +74|55 +74|17 +74|51 +74|89 +74|88 +74|96 +74|72 +74|36 +74|62 +74|68 +74|48 +74|31 +74|76 +74|45 +74|78 +57|68 +57|58 +57|97 +57|28 +57|42 +57|96 +57|76 +57|11 +57|65 +57|72 +57|17 +57|45 +57|62 +57|79 +57|35 +57|31 +57|16 +57|87 +57|66 +57|88 +57|26 +57|55 +12|38 +12|92 +12|93 +12|57 +12|82 +12|45 +12|76 +12|36 +12|11 +12|74 +12|51 +12|41 +12|35 +12|71 +12|79 +12|17 +12|62 +12|89 +12|28 +12|48 +12|86 +12|69 +12|68 +99|69 +99|74 +99|92 +99|82 +99|39 +99|46 +99|28 +99|11 +99|86 +99|57 +99|85 +99|12 +99|71 +99|75 +99|41 +99|64 +99|89 +99|81 +99|93 +99|95 +99|36 +99|51 +99|38 +99|61 +38|62 +38|79 +38|65 +38|28 +38|68 +38|48 +38|35 +38|74 +38|71 +38|31 +38|36 +38|93 +38|76 +38|26 +38|51 +38|92 +38|45 +38|69 +38|17 +38|89 +38|11 +38|82 +38|57 +38|87 +89|31 +89|36 +89|45 +89|66 +89|62 +89|76 +89|55 +89|65 +89|26 +89|79 +89|96 +89|16 +89|87 +89|58 +89|11 +89|78 +89|57 +89|68 +89|88 +89|35 +89|17 +89|28 +89|48 +89|72 +55|46 +55|86 +55|12 +55|75 +55|95 +55|41 +55|61 +55|42 +55|23 +55|94 +55|93 +55|81 +55|39 +55|99 +55|64 +55|85 +55|16 +55|88 +55|72 +55|66 +55|97 +55|38 +55|58 +55|82 +51|11 +51|36 +51|58 +51|55 +51|76 +51|26 +51|79 +51|28 +51|78 +51|68 +51|96 +51|48 +51|31 +51|65 +51|87 +51|35 +51|62 +51|88 +51|45 +51|72 +51|71 +51|17 +51|89 +51|57 +92|55 +92|87 +92|31 +92|76 +92|74 +92|17 +92|48 +92|68 +92|57 +92|96 +92|11 +92|65 +92|69 +92|26 +92|28 +92|51 +92|89 +92|79 +92|35 +92|36 +92|45 +92|71 +92|78 +92|62 +41|82 +41|76 +41|71 +41|48 +41|57 +41|74 +41|65 +41|89 +41|86 +41|17 +41|79 +41|11 +41|35 +41|93 +41|62 +41|38 +41|45 +41|36 +41|31 +41|68 +41|51 +41|92 +41|28 +41|69 +65|99 +65|55 +65|88 +65|48 +65|81 +65|85 +65|72 +65|31 +65|26 +65|39 +65|42 +65|58 +65|78 +65|87 +65|97 +65|16 +65|96 +65|23 +65|95 +65|66 +65|79 +65|76 +65|45 +65|94 +17|96 +17|94 +17|48 +17|26 +17|88 +17|87 +17|68 +17|35 +17|65 +17|72 +17|31 +17|16 +17|97 +17|23 +17|58 +17|79 +17|55 +17|66 +17|78 +17|42 +17|45 +17|62 +17|99 +17|76 +78|61 +78|75 +78|58 +78|85 +78|97 +78|88 +78|82 +78|81 +78|99 +78|42 +78|16 +78|23 +78|95 +78|38 +78|41 +78|39 +78|86 +78|55 +78|46 +78|64 +78|66 +78|72 +78|12 +78|94 +68|31 +68|79 +68|97 +68|87 +68|23 +68|55 +68|99 +68|26 +68|94 +68|66 +68|81 +68|42 +68|16 +68|58 +68|62 +68|72 +68|96 +68|78 +68|45 +68|76 +68|88 +68|48 +68|65 +68|35 +45|96 +45|55 +45|61 +45|85 +45|99 +45|81 +45|72 +45|39 +45|78 +45|75 +45|87 +45|66 +45|23 +45|16 +45|58 +45|97 +45|94 +45|26 +45|31 +45|46 +45|88 +45|64 +45|95 +45|42 +66|92 +66|12 +66|16 +66|75 +66|85 +66|42 +66|93 +66|94 +66|69 +66|82 +66|74 +66|97 +66|86 +66|46 +66|38 +66|61 +66|23 +66|64 +66|95 +66|99 +66|41 +66|51 +66|81 +66|39 +96|95 +96|94 +96|64 +96|58 +96|97 +96|81 +96|46 +96|72 +96|78 +96|23 +96|16 +96|88 +96|39 +96|55 +96|41 +96|75 +96|38 +96|12 +96|66 +96|99 +96|42 +96|61 +96|86 +96|85 +93|74 +93|78 +93|87 +93|11 +93|71 +93|57 +93|45 +93|28 +93|48 +93|69 +93|65 +93|92 +93|17 +93|35 +93|51 +93|26 +93|89 +93|31 +93|68 +93|79 +93|62 +93|96 +93|76 +93|36 +42|97 +42|92 +42|95 +42|38 +42|94 +42|82 +42|89 +42|85 +42|86 +42|69 +42|71 +42|81 +42|12 +42|23 +42|99 +42|74 +42|41 +42|64 +42|51 +42|61 +42|93 +42|75 +42|39 +42|46 +31|23 +31|26 +31|75 +31|64 +31|87 +31|88 +31|16 +31|96 +31|42 +31|85 +31|46 +31|72 +31|12 +31|61 +31|95 +31|97 +31|78 +31|58 +31|66 +31|99 +31|55 +31|94 +31|39 +31|81 +23|64 +23|39 +23|99 +23|57 +23|46 +23|82 +23|61 +23|81 +23|95 +23|85 +23|74 +23|89 +23|41 +23|12 +23|71 +23|92 +23|38 +23|86 +23|69 +23|51 +23|94 +23|93 +23|36 +23|75 +86|68 +86|62 +86|57 +86|35 +86|31 +86|17 +86|51 +86|28 +86|45 +86|36 +86|76 +86|65 +86|74 +86|89 +86|71 +86|82 +86|69 +86|93 +86|48 +86|87 +86|11 +86|92 +86|79 +86|38 +71|72 +71|68 +71|79 +71|45 +71|26 +71|57 +71|87 +71|48 +71|88 +71|78 +71|17 +71|89 +71|76 +71|96 +71|55 +71|11 +71|62 +71|58 +71|66 +71|36 +71|28 +71|31 +71|65 +71|35 +58|39 +58|82 +58|16 +58|46 +58|38 +58|69 +58|12 +58|95 +58|86 +58|97 +58|42 +58|41 +58|66 +58|23 +58|74 +58|93 +58|99 +58|75 +58|92 +58|61 +58|81 +58|85 +58|94 +58|64 +87|41 +87|42 +87|61 +87|66 +87|16 +87|99 +87|46 +87|58 +87|75 +87|96 +87|39 +87|78 +87|94 +87|97 +87|55 +87|85 +87|23 +87|95 +87|88 +87|72 +87|81 +87|26 +87|64 +87|12 +64|57 +64|61 +64|41 +64|36 +64|51 +64|46 +64|12 +64|86 +64|68 +64|38 +64|93 +64|11 +64|35 +64|74 +64|69 +64|28 +64|17 +64|65 +64|89 +64|71 +64|82 +64|79 +64|92 +64|62 +46|65 +46|69 +46|48 +46|38 +46|28 +46|17 +46|51 +46|79 +46|74 +46|62 +46|11 +46|36 +46|68 +46|71 +46|35 +46|41 +46|82 +46|93 +46|89 +46|86 +46|92 +46|76 +46|12 +46|57 +72|58 +72|38 +72|39 +72|85 +72|23 +72|46 +72|99 +72|66 +72|81 +72|42 +72|82 +72|41 +72|86 +72|93 +72|94 +72|16 +72|75 +72|12 +72|97 +72|64 +72|88 +72|92 +72|61 +72|95 +94|86 +94|46 +94|12 +94|71 +94|89 +94|74 +94|93 +94|75 +94|57 +94|85 +94|64 +94|36 +94|39 +94|81 +94|99 +94|41 +94|69 +94|28 +94|38 +94|95 +94|61 +94|51 +94|92 +94|82 +48|96 +48|94 +48|45 +48|87 +48|75 +48|95 +48|81 +48|64 +48|66 +48|78 +48|58 +48|97 +48|16 +48|85 +48|72 +48|55 +48|23 +48|31 +48|76 +48|88 +48|99 +48|26 +48|42 +26|66 +26|86 +26|61 +26|96 +26|55 +26|23 +26|39 +26|16 +26|46 +26|41 +26|42 +26|58 +26|72 +26|64 +26|12 +26|95 +26|75 +26|99 +26|78 +26|81 +26|85 +26|88 +76|26 +76|39 +76|81 +76|45 +76|99 +76|87 +76|61 +76|96 +76|78 +76|94 +76|72 +76|42 +76|55 +76|97 +76|85 +76|95 +76|23 +76|64 +76|16 +76|75 +76|31 +75|17 +75|38 +75|11 +75|69 +75|35 +75|93 +75|86 +75|65 +75|92 +75|28 +75|12 +75|41 +75|82 +75|68 +75|62 +75|46 +75|36 +75|64 +75|89 +75|51 +69|26 +69|57 +69|48 +69|74 +69|87 +69|71 +69|17 +69|78 +69|31 +69|45 +69|11 +69|76 +69|55 +69|62 +69|68 +69|28 +69|72 +69|35 +69|79 +62|94 +62|31 +62|58 +62|88 +62|96 +62|45 +62|78 +62|48 +62|95 +62|87 +62|23 +62|85 +62|55 +62|42 +62|16 +62|26 +62|97 +62|66 +82|17 +82|26 +82|74 +82|36 +82|48 +82|31 +82|11 +82|79 +82|96 +82|93 +82|68 +82|28 +82|51 +82|65 +82|71 +82|76 +82|45 +28|58 +28|87 +28|55 +28|11 +28|42 +28|65 +28|23 +28|62 +28|16 +28|45 +28|97 +28|96 +28|76 +28|17 +28|48 +28|88 +16|23 +16|41 +16|81 +16|42 +16|86 +16|92 +16|74 +16|93 +16|64 +16|71 +16|95 +16|94 +16|97 +16|39 +16|69 +39|11 +39|41 +39|17 +39|75 +39|93 +39|82 +39|28 +39|71 +39|69 +39|12 +39|92 +39|51 +39|61 +39|62 +36|11 +36|96 +36|88 +36|45 +36|31 +36|78 +36|58 +36|26 +36|66 +36|68 +36|28 +36|79 +36|72 +35|99 +35|79 +35|55 +35|16 +35|96 +35|88 +35|66 +35|45 +35|72 +35|31 +35|62 +35|81 +81|36 +81|92 +81|69 +81|61 +81|11 +81|28 +81|74 +81|95 +81|46 +81|85 +81|38 +88|94 +88|81 +88|39 +88|86 +88|85 +88|61 +88|64 +88|23 +88|99 +88|58 +11|97 +11|87 +11|78 +11|72 +11|23 +11|48 +11|31 +11|55 +11|96 +95|28 +95|93 +95|92 +95|12 +95|64 +95|71 +95|89 +95|75 +85|46 +85|69 +85|86 +85|92 +85|39 +85|17 +85|93 +97|41 +97|94 +97|71 +97|39 +97|86 +97|99 +61|48 +61|74 +61|57 +61|35 +61|69 +79|72 +79|58 +79|42 +79|78 +74|87 +74|35 +74|65 +57|78 +57|48 +12|65 + +74,57,28,17,96 +26,58,55,62,76,23,66,97,45,88,94,68,16,99,79,87,65,42,72,78,48,35,96 +62,65,48,76,45,87,26,96,78,55,72,88,58,66,16,42,97,94,99,81,85 +17,62,38,11,69,74,46,51,68,92,89,82,12,71,36,79,65,93,35,57,41,28,86 +62,87,88,66,16,97,85 +11,89,17,87,62,55,69,65,76 +55,58,66,23,94,81,39,75,64,61,46,86,82 +58,66,42,97,94,81,95,75,64,61,46,12,41,86,38,82,93,92,69 +28,11,17,68,35,62,45,31,26,96,78,55,72,66,16,42,97 +41,86,38,82,93,69,74,89,36,57,28,17,62,65,45 +36,99,61,82,64,74,38,94,89,39,95,41,92,93,85,69,12,75,23 +41,51,64,12,92,69,36,46,11,61,86,74,38,85,28,89,95,82,39,75,57,17,93 +58,42,94,99,85,39,69 +28,65,36,51,79,57,87,62,71,38,69,31,45 +17,11,97,79,35,16,26,48,42,65,72,31,23 +39,75,64,41,86,38,92,69,89,36,35 +23,94,99,85,95,39,75,46,86,38,82,92,69,74,36 +61,46,12,41,86,38,93,92,69,74,51,89,36,57,28,11,17,35,62,65,79 +64,82,61,41,75,86,12,81,92,51,39,42,16,95,74 +72,31,66,26,78,85,16,97,23,39,95,45,79 +16,42,94,81,95,46,86,93,92,69,51 +71,36,68,62,79,48,72 +85,95,64,61,93,92,51,36,17 +38,68,69,57,17,76,71,74,36,62,92,35,28,45,48,65,89,82,11,79,93,51,41 +38,93,92,69,74,51,71,89,57,28,11,68,35,62,65,79,48,76,45,31,87 +17,38,64,41,51,46,35,62,11,82,65,92,36 +35,48,45,72,88,66,42,97,81 +61,96,81,85,64 +48,58,88,96,42,95,31,99,72,66,85,39,55,76,23,87,16,97,81,78,75,26,94 +78,64,58,81,88,39,55,96,97,66,85,46,75,86,42 +99,85,88,31,79,62,81 +12,82,92,69,74,89,36,57,28,11,68,65,76 +26,89,55,11,31,76,72,45,88,57,79,58,66 +99,81,85,38,89,57,28 +74,51,71,36,57,28,11,17,68,35,62,65,79,48,76,45,31,87,26,96,78,55,72 +11,51,26,74,87,62,65,17,76,31,72 +46,71,11,35,62 +88,61,23,85,38,94,64,66,97 +64,12,38,58,81,86,46,95,55,61,16,42,78 +95,97,78,88,55,96,81,42,48,31,23,45,72,85,99 +99,95,75,64,61,86,38,69,71,89,57 +45,26,96,78,55,72,88,58,16,23,94,99,75,64,61 +45,78,55,66,94 +39,75,64,61,46,12,41,86,38,82,93,92,69,74,51,71,89,36,57,11,17,68,35 +94,38,12,71,23,69,75,86,74,41,99,81,92,42,51,85,93,82,97 +39,38,82,89,36 +39,75,64,61,46,41,86,38,92,69,71,89,36,57,28,11,17,68,35 +87,11,89,69,65,17,76,96,48,51,79,92,31,26,45,74,71,35,62,28,68,57,78 +48,76,31,96,55,88,58,66,42,99,95,39,75 +31,78,55,88,95,64,46 +93,74,51,71,89,36,11,35,62,65,79,45,87,26,96 +46,74,85,36,61,23,89,64,86,75,99,81,69 +81,95,61,46,38,82,74,89,57,28,11 +99,95,39,75,64,61,41,86,93,69,74,51,71,89,36,57,28 +28,17,65,48,76,31,96,55,58,66,16,42,97 +26,96,99,66,94,23,85,87,79,39,55,88,45,16,42,31,58 +11,31,96,78,72,66,23 +85,95,61,12,86,38,82,93,51,71,36,28,11 +66,16,23,94,99,85,95,39,64,61,46,12,41,86,38,82,93,92,74 +79,76,45,96,55,81,39 +41,93,36,57,11 +65,88,48,62,31,57,87,96,16,45,28,17,11,35,55,58,66,42,68,26,78,72,76 +48,36,11,35,12,93,51,89,68,28,92,76,57 +69,36,71,28,31,76,89,65,57,87,48,26,68,55,51 +92,12,39,99,66,95,82,88,97,93,61,58,75,23,38,42,81 +92,68,41,35,12,64,38,11,57,36,65,93,89 +36,28,11,17,35,62,65,79,48,76,26,96,78,55,72,58,16 +78,72,79,35,88,96,76 +26,62,72,74,78,45,35 +71,89,36,57,28,11,17,68,35,62,79,48,76,45,31,87,26,96,78,55,72,88,58 +36,57,74,99,75,69,89,71,51,86,82,94,38 +82,93,92,69,74,51,71,36,28,68,35,62,65,48,76,45,31,87,26 +97,23,94,99,81,85,95,39,75,64,46,12,41,86,38,82,93,92,69,74,51,71,89 +97,94,64,46,39,23,61 +51,71,89,36,57,28,11,35,65,79,76,45,31,87,26,96,78,55,88 +58,81,95,72,26,64,39,55,97,87,16,88,85,99,96,76,45,94,66 +26,96,78,66,42,97,94,81,85,95,75,61,41 +39,64,61,46,12,41,38,92,69,74,51,71,89,57,28,11,35 +11,68,35,78,55,72,88,16,23 +28,12,46,35,62,17,69,41,36,71,11,92,61,68,57,75,93,82,64,74,86 +51,36,28,11,79,76,31,87,26,78,55,72,88 +26,96,78,55,72,88,66,42,97,23,99,95,39,75,64,61,46,12,41 +94,99,85,39,64,86,38,92,69,74,51,71,89 +39,96,99,66,16,95,42,76,94,64,87 +82,92,57,11,68,45,26 +88,64,58,72,81,96,26,94,78,75,99,31,46,95,39,85,42,16,61 +89,36,57,28,11,17,62,65,79,48,76,45,31,87,26,96,78,55,72,58,66 +82,93,92,69,74,51,89,36,57,28,11,17,68,62,65,79,48,76,45,87,26 +38,85,81,75,88,92,93,23,42,66,39,41,12,58,97,95,94 +64,61,12,86,38,82,93,92,69,74,51,71,89,36,57,28,11,68,35,62,65 +97,81,85,46,12,41,82,93,71 +81,76,72,96,78,66,23,85,65,87,31,26,58 +94,82,71,39,95,36,61,23,93,41,46 +92,74,51,36,57,28,62,65,31,26,78 +23,94,95,39,64,12,86,38,82,93,92,69,74,51,71,89,36 +74,51,71,89,36,28,68,35,65,76,26,96,72 +86,38,64,94,78,72,23 +38,82,89,36,28,11,17,62,48,76,31 +92,57,71,95,74,46,61,36,41,38,69,82,86,17,64,89,12,39,93,11,28,75,68 +26,87,45,62,78,68,28 +11,35,72,88,66,42,23 +31,87,96,94,42,81,76,45,66,99,64 +74,79,78,31,87,65,11,76,51,96,62,28,89,48,71,92,17,68,69,45,36 +94,85,39,64,82,92,51,89,57 +12,41,86,82,93,92,36,57,28,17,68,35,65,79,76 +46,95,12,36,99,39,57,93,94 +69,41,68,89,48,11,62,86,45,57,74,71,76,38,28,82,36,93,35 +55,88,16,97,94,99,81,95,39,64,61,41,86,38,82 +88,58,66,42,97,23,94,99,81,85,75,64,61,46,12,41,86 +57,28,11,17,68,65,48,76,31,87,96,55,72,66,16 +58,42,94,81,85,95,64,12,41,86,82,93,92 +96,55,42,99,39,12,86 +74,93,69,17,75,92,38 +45,87,96,55,72,95,39,75,61 +39,61,46,92,74,71,36,28,17 +81,46,41,38,93,74,51,71,89 +62,65,79,48,31,87,26,96,78,55,72,88,58,66,16,42,97,23,99,81,85 +38,28,41,89,64,61,95,39,36,74,93,46,86,82,11,12,71,17,75 +46,58,66,87,85,23,42,97,88,94,26,31,78 +41,82,11,74,85,93,89,86,75,12,38,17,39,69,46,71,92 +72,16,64,61,46,41,93 +57,28,11,68,35,62,65,79,48,76,45,87,26,96,78,55,88,58,66,16,42 +76,55,48,68,69,74,79,17,96 +76,45,31,26,72,88,58,66,16,42,97,23,94,99,85,95,64 +17,65,79,45,26,96,78,55,88 +69,36,57,17,68,62,65,76,31,26,96 +41,86,38,82,92,69,74,51,89,36,57,11,17,68,35,79,48,76,45 +42,99,81,95,39,69,71 +46,12,41,38,82,93,74,51,71,89,36,57,28,35,62 +72,88,66,16,42,23,94,99,81,85,95,75,64,46,41,86,38,82,93 +48,31,87,88,42,94,81,85,75 +75,64,61,46,12,41,38,82,93,92,69,51,71,89,36,57,11,17,68,35,62 +82,93,92,69,74,51,71,89,28,17,35,65,45,31,26 +65,48,76,87,26,96,78,55,88,58,66,16,42,97,23,94,81,85,95 +61,66,94,88,87,23,12,16,75,26,97 +23,97,66,39,76,95,26,31,42,16,78,96,75,81,94,55,88,72,48,85,87 +55,88,16,94,81,75,64 +95,39,75,64,61,12,86,69,51,89,36,28,11,17,68 +93,92,74,71,89,36,28,11,17,35,62,65,79,31,87,26,96 +65,79,48,76,26,78,55,58,66,97,94,99,81,85,95 +66,12,87,94,97,39,75,95,96 +28,11,17,35,62,65,79,48,76,45,31,87,26,96,78,55,72,88,58,66,16,42,97 +35,62,79,76,87,26,66,99,81 +51,89,36,28,11,17,35,65,79,45,26,96,78,55,88 +51,96,28,45,65,76,26,11,92,36,89,93,57,74,69,48,17,71,87,35,31,68,62 +11,68,79,45,31,26,78,58,16,97,23 +23,94,85,95,75,64,61,46,82,92,74,71,36 +89,57,92,71,68,62,38,86,36 +68,35,48,31,78,16,42,23,99 +39,42,75,85,94 +79,96,72,58,16,97,39 +72,41,66,97,95,61,64,46,88,86,12,16,85,82,93,23,58,81,42,94,38,39,99 +12,41,86,38,82,93,92,69,74,51,71,89,36,57,28,11,68,35,62,65,79,48,76 +26,11,74,51,82,93,17,69,71,28,68,89,57,31,45,62,87,35,79 +62,66,55,35,57,72,79,28,31,48,17,88,78,65,42,68,58,16,26,87,76,96,11 +55,58,95,78,88,85,65,81,76,99,45,66,96,79,42,72,26,97,94 +92,69,74,71,89,36,57,28,11,17,35,62,65,48,76,45,31,87,96 +85,88,61,23,16,39,93,86,75,46,92 +88,58,66,16,42,23,99,95,39,75,64,61,46,12,86,38,82,93,92 +46,12,86,38,82,92,69,74,36,57,11,17,48 +28,48,87,55,79,51,68,36,72,26,65,11,96,17,74,31,57,78,35,71,62,89,45 +85,95,39,64,61,46,12,41,86,38,82,93,92,69,74,51,71,57,28,11,17 +51,28,89,69,11 +92,95,89,74,69,28,38,71,99,12,61 +86,57,71,41,95,39,85 +89,36,57,28,11,17,68,35,65,48,45,31,87,78,55,58,66 +69,28,46,64,35,74,93,65,89 +36,79,74,65,86,31,76,35,28 +62,65,79,48,76,31,87,26,96,55,72,88,66,16,42,23,94,99,85 +39,61,12,41,86,93,92,51,71,36,28,68,35 +38,12,81,28,86 +65,45,31,78,99,81,95 +58,42,97,23,99,81,95,75,64,61,41,92,69 +88,89,62,36,11,17,78,58,76,71,96 +26,78,62,35,97,28,72,76,96,68,45,87,11,66,65,48,79,55,42,16,88 +86,51,71,89,17,35,62,79,48,45,31 +97,23,94,85,61,74,71 +76,45,26,96,78,55,72,88,16,42,97,23,94,99,81,85,75 +68,35,79,48,76,26,58,16,97 +65,45,26,96,55,72,88,16,94,99,81 +71,17,68,35,79,48,31,26,78,55,72,88,58 +97,42,41,72,39,95,55 +28,11,17,62,65,48,45,87,26,96,55,72,58,42,97 +41,69,86,92,62,79,61,82,89,71,35,12,38,57,46,17,51 +62,65,76,45,31,87,26,96,55,72,88,58,16,97,23 +79,48,76,87,55,72,88,58,66,42,97,23,94,99,81,85,39 +35,65,48,78,55,72,88,42,97,99,81 +75,46,12,41,92,74,51,89,57,28,11,17,35 +55,72,88,58,16,97,23,94,81,95,39,75,64,12,41 +66,16,42,97,23,99,81,85,39,75,64,61,46,12,41,38,82,93,92,69,74 +16,99,94,48,97 +69,74,51,71,89,36,57,28,11,17,68,35,65,79,48,76,45,31,87,26,96,78,55 +99,94,16,86,23,72,78,96,12,97,42 +78,85,55,75,86,94,72 +78,61,64,99,66,97,23,88,46,12,94,95,85,55,16,87,72,96,39 +61,64,66,41,12,69,86,93,85,38,95,46,39,99,92,81,97,16,23 +93,35,82,31,11,57,17,86,92,68,51,48,71 +42,99,85,95,64,46,86,74,71 +41,86,38,93,92,69,74,71,89,36,11,17,35,62,65,79,45 +88,92,58,85,97,66,46 +95,16,94,86,46,64,92,61,42,99,69 +58,95,86,85,64,38,55,12,99,39,23,46,97,82,42 +88,99,12,46,82,55,95 +65,62,16,35,88,78,17,66,45,97,94,42,68,76,72 diff --git a/day05/manifest.toml b/day05/manifest.toml new file mode 100644 index 0000000..207fe49 --- /dev/null +++ b/day05/manifest.toml @@ -0,0 +1,14 @@ +# This file was generated by Gleam +# You typically do not need to edit this file + +packages = [ + { name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" }, + { name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" }, + { name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" }, + { name = "simplifile", version = "2.2.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0DFABEF7DC7A9E2FF4BB27B108034E60C81BEBFCB7AB816B9E7E18ED4503ACD8" }, +] + +[requirements] +gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" } +gleeunit = { version = ">= 1.0.0 and < 2.0.0" } +simplifile = { version = ">= 2.2.0 and < 3.0.0" } diff --git a/day05/small-in.txt b/day05/small-in.txt new file mode 100644 index 0000000..9d146d6 --- /dev/null +++ b/day05/small-in.txt @@ -0,0 +1,28 @@ +47|53 +97|13 +97|61 +97|47 +75|29 +61|13 +75|53 +29|13 +97|29 +53|29 +61|53 +97|53 +61|29 +47|13 +75|47 +97|75 +47|61 +75|61 +47|29 +75|13 +53|13 + +75,47,61,53,29 +97,61,53,29,13 +75,29,13 +75,97,47,61,53 +61,13,29 +97,13,75,29,47 diff --git a/day05/src/day05.gleam b/day05/src/day05.gleam new file mode 100644 index 0000000..118038a --- /dev/null +++ b/day05/src/day05.gleam @@ -0,0 +1,110 @@ +import gleam/bool +import gleam/int +import gleam/io +import gleam/list +import gleam/result +import gleam/set +import gleam/string +import simplifile + +type Reqs = + set.Set(#(Int, Int)) + +fn force_unwrap(r: Result(a, e)) -> a { + result.lazy_unwrap(r, fn() { panic }) +} + +fn parse_int(s: String) -> Int { + int.parse(s) |> force_unwrap +} + +fn make_requirements(lines: List(String)) -> Reqs { + lines + |> list.map(fn(line) { + let assert [left, right] = string.split(line, "|") |> list.map(parse_int) + #(left, right) + }) + |> set.from_list +} + +fn check_update(update: List(Int), reqs: Reqs) -> Bool { + list.combination_pairs(update) + |> list.all(fn(p) { !set.contains(reqs, #(p.1, p.0)) }) +} + +fn middle_element(l: List(a)) -> a { + let len = list.length(l) + use <- bool.lazy_guard(len % 2 == 1, otherwise: fn() { panic }) + + let mid = len / 2 + list.drop(l, mid) |> list.first |> force_unwrap +} + +type Input { + Input(reqs: Reqs, good: List(List(Int)), bad: List(List(Int))) +} + +fn parse_input(input: String) -> Input { + let assert [rules, updates] = + input + |> string.split("\n\n") + |> list.map(string.split(_, "\n")) + + let reqs = make_requirements(rules) + + let #(good, bad) = + updates + |> list.map(fn(update) { string.split(update, ",") |> list.map(parse_int) }) + |> list.partition(check_update(_, reqs)) + + Input(reqs, good, bad) +} + +pub fn part1(input: String) -> Int { + parse_input(input).good + |> list.map(middle_element) + |> int.sum +} + +type Acc { + Acc(res: List(Int), seen: set.Set(Int)) +} + +// Recursive, dfs-based topsort 🤯 +fn rec(update: List(Int), reqs: Reqs, curr_node: Int, acc: Acc) { + use <- bool.guard(set.contains(acc.seen, curr_node), return: acc) + let acc = Acc(..acc, seen: set.insert(acc.seen, curr_node)) + + let acc = + list.fold(update, acc, fn(acc, neigh) { + use <- bool.guard(set.contains(reqs, #(neigh, curr_node)), return: acc) + rec(update, reqs, neigh, acc) + }) + + Acc(..acc, res: [curr_node, ..acc.res]) +} + +fn reorder_update(update: List(Int), reqs: Reqs) -> List(Int) { + list.fold(update, Acc([], set.new()), fn(acc, curr) { + use <- bool.guard(set.contains(acc.seen, curr), return: acc) + rec(update, reqs, curr, acc) + }).res +} + +pub fn part2(input: String) -> Int { + let Input(reqs, _, bad) = parse_input(input) + bad + |> list.map(reorder_update(_, reqs)) + |> list.map(middle_element) + |> int.sum +} + +pub fn main() { + let input = + simplifile.read("input.txt") + |> result.lazy_unwrap(fn() { panic }) + |> string.trim + + io.println("Part 1: " <> string.inspect(part1(input))) + io.println("Part 2: " <> string.inspect(part2(input))) +} diff --git a/day05/test/day05_test.gleam b/day05/test/day05_test.gleam new file mode 100644 index 0000000..32a5056 --- /dev/null +++ b/day05/test/day05_test.gleam @@ -0,0 +1,27 @@ +import gleam/result +import gleam/string +import gleeunit +import gleeunit/should +import simplifile + +import day05 + +fn input() -> String { + simplifile.read("small-in.txt") + |> result.lazy_unwrap(fn() { panic }) + |> string.trim +} + +pub fn main() { + gleeunit.main() +} + +pub fn part1_test() { + day05.part1(input()) + |> should.equal(143) +} + +pub fn part2_test() { + day05.part2(input()) + |> should.equal(123) +}