diff --git a/aoc2024.cabal b/aoc2024.cabal index 4d95d27..23b4720 100644 --- a/aoc2024.cabal +++ b/aoc2024.cabal @@ -28,6 +28,7 @@ library Day1 Day2 Day3 + Day5 other-modules: Paths_aoc2024 autogen-modules: @@ -37,7 +38,9 @@ library ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints build-depends: base >=4.7 && <5 + , containers , regex-tdfa >=1.3.2 && <1.4 + , text >=2.0.2 default-language: Haskell2010 test-suite aoc2024-test @@ -47,6 +50,7 @@ test-suite aoc2024-test Day1Spec Day2Spec Day3Spec + Day5Spec Paths_aoc2024 autogen-modules: Paths_aoc2024 @@ -57,6 +61,8 @@ test-suite aoc2024-test QuickCheck >=2.14.3 && <2.15 , aoc2024 , base >=4.7 && <5 + , containers , hspec >=2.0.0 , regex-tdfa >=1.3.2 && <1.4 + , text >=2.0.2 default-language: Haskell2010 diff --git a/input/day5.txt b/input/day5.txt new file mode 100644 index 0000000..c0a9534 --- /dev/null +++ b/input/day5.txt @@ -0,0 +1,1366 @@ +48|39 +39|84 +39|23 +95|51 +95|76 +95|61 +14|52 +14|49 +14|39 +14|53 +85|19 +85|25 +85|61 +85|35 +85|58 +74|86 +74|55 +74|49 +74|18 +74|83 +74|47 +45|49 +45|76 +45|86 +45|22 +45|35 +45|18 +45|37 +52|58 +52|16 +52|23 +52|12 +52|31 +52|27 +52|46 +52|15 +49|71 +49|44 +49|54 +49|91 +49|58 +49|25 +49|52 +49|84 +49|46 +86|26 +86|95 +86|52 +86|51 +86|48 +86|91 +86|55 +86|22 +86|11 +86|37 +69|18 +69|86 +69|98 +69|34 +69|79 +69|95 +69|22 +69|27 +69|15 +69|65 +69|37 +66|83 +66|65 +66|18 +66|92 +66|48 +66|32 +66|16 +66|47 +66|22 +66|95 +66|37 +66|49 +15|22 +15|83 +15|37 +15|49 +15|65 +15|85 +15|31 +15|95 +15|79 +15|47 +15|74 +15|16 +15|86 +71|16 +71|66 +71|92 +71|84 +71|15 +71|54 +71|25 +71|53 +71|31 +71|98 +71|46 +71|69 +71|74 +71|58 +79|49 +79|34 +79|86 +79|26 +79|18 +79|14 +79|22 +79|91 +79|77 +79|48 +79|37 +79|52 +79|95 +79|35 +79|74 +76|39 +76|84 +76|94 +76|77 +76|12 +76|24 +76|46 +76|69 +76|15 +76|53 +76|23 +76|19 +76|52 +76|11 +76|66 +76|25 +19|94 +19|79 +19|71 +19|92 +19|66 +19|16 +19|53 +19|45 +19|69 +19|31 +19|24 +19|25 +19|12 +19|54 +19|84 +19|58 +19|98 +11|25 +11|27 +11|84 +11|12 +11|24 +11|66 +11|19 +11|51 +11|69 +11|16 +11|53 +11|94 +11|58 +11|44 +11|23 +11|39 +11|35 +11|52 +92|37 +92|91 +92|31 +92|48 +92|76 +92|14 +92|34 +92|55 +92|32 +92|85 +92|86 +92|98 +92|83 +92|49 +92|18 +92|26 +92|95 +92|11 +92|74 +23|16 +23|18 +23|86 +23|74 +23|92 +23|66 +23|79 +23|55 +23|45 +23|47 +23|26 +23|37 +23|15 +23|24 +23|12 +23|98 +23|34 +23|48 +23|65 +23|27 +37|51 +37|61 +37|26 +37|47 +37|49 +37|84 +37|52 +37|34 +37|55 +37|39 +37|14 +37|19 +37|77 +37|35 +37|58 +37|91 +37|83 +37|76 +37|85 +37|11 +37|32 +55|44 +55|19 +55|52 +55|39 +55|84 +55|54 +55|32 +55|47 +55|71 +55|51 +55|83 +55|35 +55|91 +55|85 +55|76 +55|11 +55|58 +55|61 +55|53 +55|49 +55|25 +55|77 +27|48 +27|18 +27|65 +27|34 +27|74 +27|37 +27|47 +27|98 +27|55 +27|83 +27|79 +27|16 +27|86 +27|85 +27|45 +27|66 +27|22 +27|49 +27|15 +27|26 +27|14 +27|92 +27|95 +51|16 +51|84 +51|54 +51|77 +51|27 +51|94 +51|66 +51|23 +51|19 +51|25 +51|71 +51|65 +51|15 +51|52 +51|39 +51|44 +51|46 +51|24 +51|12 +51|31 +51|92 +51|69 +51|58 +51|53 +83|32 +83|52 +83|35 +83|76 +83|94 +83|44 +83|84 +83|14 +83|71 +83|77 +83|61 +83|12 +83|85 +83|23 +83|39 +83|51 +83|19 +83|54 +83|25 +83|49 +83|11 +83|53 +83|58 +83|91 +94|66 +94|15 +94|74 +94|12 +94|55 +94|86 +94|16 +94|69 +94|26 +94|45 +94|34 +94|65 +94|98 +94|18 +94|23 +94|48 +94|46 +94|92 +94|37 +94|31 +94|22 +94|27 +94|24 +94|79 +54|15 +54|26 +54|92 +54|46 +54|69 +54|22 +54|23 +54|66 +54|24 +54|74 +54|31 +54|37 +54|16 +54|48 +54|34 +54|94 +54|65 +54|79 +54|45 +54|18 +54|12 +54|27 +54|86 +54|98 +58|23 +58|31 +58|48 +58|25 +58|79 +58|65 +58|15 +58|24 +58|16 +58|54 +58|86 +58|22 +58|94 +58|46 +58|27 +58|12 +58|45 +58|69 +58|92 +58|74 +58|66 +58|44 +58|98 +58|18 +61|65 +61|46 +61|54 +61|19 +61|77 +61|52 +61|39 +61|25 +61|66 +61|12 +61|24 +61|94 +61|44 +61|51 +61|27 +61|15 +61|69 +61|71 +61|23 +61|84 +61|58 +61|92 +61|16 +61|53 +77|53 +77|84 +77|24 +77|58 +77|65 +77|12 +77|31 +77|92 +77|46 +77|23 +77|66 +77|27 +77|25 +77|39 +77|19 +77|71 +77|16 +77|45 +77|52 +77|94 +77|69 +77|44 +77|54 +77|15 +32|35 +32|61 +32|51 +32|71 +32|91 +32|53 +32|46 +32|39 +32|52 +32|11 +32|23 +32|77 +32|76 +32|12 +32|27 +32|54 +32|94 +32|19 +32|58 +32|44 +32|25 +32|24 +32|69 +32|84 +31|85 +31|79 +31|35 +31|91 +31|86 +31|18 +31|76 +31|98 +31|55 +31|45 +31|95 +31|11 +31|49 +31|22 +31|14 +31|74 +31|34 +31|32 +31|26 +31|48 +31|83 +31|61 +31|37 +31|47 +34|26 +34|77 +34|85 +34|47 +34|61 +34|52 +34|19 +34|11 +34|58 +34|91 +34|35 +34|95 +34|39 +34|25 +34|14 +34|53 +34|71 +34|84 +34|76 +34|51 +34|83 +34|55 +34|49 +34|32 +46|22 +46|26 +46|98 +46|27 +46|45 +46|15 +46|83 +46|86 +46|24 +46|18 +46|65 +46|79 +46|34 +46|16 +46|55 +46|74 +46|95 +46|66 +46|92 +46|37 +46|47 +46|48 +46|69 +46|31 +26|53 +26|95 +26|35 +26|47 +26|19 +26|76 +26|83 +26|55 +26|11 +26|51 +26|77 +26|52 +26|39 +26|71 +26|91 +26|44 +26|32 +26|84 +26|85 +26|14 +26|49 +26|61 +26|25 +26|58 +91|24 +91|23 +91|94 +91|69 +91|46 +91|27 +91|76 +91|58 +91|53 +91|11 +91|19 +91|39 +91|52 +91|61 +91|84 +91|51 +91|25 +91|54 +91|77 +91|35 +91|44 +91|12 +91|15 +91|71 +53|48 +53|69 +53|44 +53|94 +53|92 +53|66 +53|79 +53|98 +53|31 +53|27 +53|45 +53|15 +53|16 +53|22 +53|12 +53|86 +53|54 +53|24 +53|58 +53|23 +53|74 +53|46 +53|65 +53|25 +12|74 +12|16 +12|65 +12|66 +12|95 +12|31 +12|34 +12|37 +12|47 +12|24 +12|98 +12|92 +12|15 +12|22 +12|69 +12|86 +12|27 +12|46 +12|55 +12|26 +12|45 +12|18 +12|79 +12|48 +24|34 +24|15 +24|26 +24|27 +24|95 +24|55 +24|37 +24|14 +24|74 +24|48 +24|31 +24|66 +24|45 +24|98 +24|18 +24|86 +24|92 +24|16 +24|65 +24|47 +24|83 +24|79 +24|22 +24|85 +84|54 +84|98 +84|53 +84|46 +84|58 +84|23 +84|22 +84|12 +84|16 +84|94 +84|45 +84|66 +84|65 +84|31 +84|86 +84|79 +84|25 +84|24 +84|69 +84|15 +84|74 +84|27 +84|92 +84|44 +18|53 +18|35 +18|55 +18|77 +18|85 +18|37 +18|83 +18|26 +18|52 +18|14 +18|39 +18|51 +18|19 +18|71 +18|11 +18|84 +18|91 +18|49 +18|32 +18|34 +18|76 +18|95 +18|61 +18|47 +25|65 +25|12 +25|94 +25|37 +25|45 +25|24 +25|92 +25|66 +25|79 +25|74 +25|22 +25|86 +25|98 +25|18 +25|44 +25|48 +25|23 +25|27 +25|16 +25|69 +25|31 +25|46 +25|54 +25|15 +98|83 +98|11 +98|22 +98|49 +98|37 +98|61 +98|85 +98|79 +98|95 +98|32 +98|35 +98|34 +98|48 +98|76 +98|77 +98|91 +98|47 +98|74 +98|86 +98|26 +98|51 +98|14 +98|18 +98|55 +22|76 +22|61 +22|47 +22|35 +22|85 +22|77 +22|51 +22|95 +22|26 +22|14 +22|37 +22|71 +22|83 +22|32 +22|55 +22|39 +22|52 +22|49 +22|91 +22|19 +22|18 +22|34 +22|11 +22|48 +47|95 +47|11 +47|14 +47|51 +47|49 +47|54 +47|39 +47|94 +47|85 +47|44 +47|61 +47|71 +47|58 +47|83 +47|19 +47|32 +47|77 +47|25 +47|84 +47|53 +47|76 +47|52 +47|35 +47|91 +35|54 +35|27 +35|77 +35|84 +35|71 +35|46 +35|12 +35|16 +35|51 +35|39 +35|52 +35|61 +35|15 +35|92 +35|94 +35|53 +35|66 +35|58 +35|44 +35|19 +35|25 +35|24 +35|23 +35|69 +65|45 +65|86 +65|95 +65|83 +65|31 +65|11 +65|35 +65|34 +65|18 +65|74 +65|32 +65|91 +65|26 +65|22 +65|49 +65|37 +65|14 +65|79 +65|55 +65|85 +65|76 +65|47 +65|98 +65|48 +44|74 +44|16 +44|86 +44|27 +44|48 +44|24 +44|94 +44|15 +44|65 +44|69 +44|66 +44|12 +44|22 +44|34 +44|23 +44|79 +44|92 +44|98 +44|31 +44|37 +44|18 +44|54 +44|45 +44|46 +16|18 +16|55 +16|92 +16|22 +16|79 +16|98 +16|26 +16|74 +16|45 +16|86 +16|76 +16|31 +16|47 +16|32 +16|83 +16|91 +16|48 +16|49 +16|85 +16|34 +16|65 +16|14 +16|95 +16|37 +48|71 +48|61 +48|52 +48|95 +48|76 +48|37 +48|91 +48|51 +48|77 +48|84 +48|34 +48|49 +48|18 +48|83 +48|11 +48|47 +48|26 +48|35 +48|32 +48|55 +48|14 +48|85 +48|19 +39|15 +39|65 +39|79 +39|19 +39|69 +39|53 +39|44 +39|31 +39|45 +39|54 +39|46 +39|92 +39|98 +39|16 +39|24 +39|58 +39|94 +39|25 +39|71 +39|66 +39|12 +39|27 +95|58 +95|11 +95|39 +95|44 +95|19 +95|94 +95|35 +95|83 +95|71 +95|91 +95|54 +95|77 +95|53 +95|52 +95|14 +95|84 +95|49 +95|85 +95|32 +95|23 +95|25 +14|35 +14|76 +14|94 +14|25 +14|46 +14|58 +14|84 +14|23 +14|11 +14|71 +14|12 +14|77 +14|32 +14|19 +14|61 +14|44 +14|54 +14|85 +14|91 +14|51 +85|53 +85|49 +85|76 +85|71 +85|44 +85|94 +85|11 +85|12 +85|39 +85|91 +85|77 +85|52 +85|54 +85|69 +85|46 +85|23 +85|51 +85|84 +85|32 +74|61 +74|95 +74|26 +74|91 +74|48 +74|14 +74|37 +74|52 +74|34 +74|39 +74|32 +74|77 +74|22 +74|51 +74|76 +74|35 +74|11 +74|85 +45|83 +45|91 +45|11 +45|51 +45|79 +45|34 +45|47 +45|98 +45|55 +45|14 +45|74 +45|48 +45|26 +45|61 +45|32 +45|85 +45|95 +52|19 +52|98 +52|54 +52|71 +52|45 +52|25 +52|65 +52|92 +52|53 +52|66 +52|39 +52|84 +52|94 +52|69 +52|24 +52|44 +49|23 +49|11 +49|39 +49|51 +49|53 +49|35 +49|61 +49|24 +49|19 +49|94 +49|12 +49|77 +49|32 +49|69 +49|76 +86|49 +86|14 +86|83 +86|32 +86|76 +86|85 +86|61 +86|77 +86|18 +86|19 +86|34 +86|35 +86|47 +86|39 +69|92 +69|16 +69|83 +69|66 +69|48 +69|24 +69|26 +69|47 +69|14 +69|45 +69|55 +69|74 +69|31 +66|26 +66|79 +66|91 +66|31 +66|34 +66|85 +66|45 +66|98 +66|86 +66|14 +66|55 +66|74 +15|45 +15|14 +15|26 +15|32 +15|18 +15|98 +15|48 +15|34 +15|66 +15|92 +15|55 +71|45 +71|24 +71|44 +71|86 +71|12 +71|79 +71|23 +71|65 +71|94 +71|27 +79|85 +79|55 +79|11 +79|83 +79|61 +79|51 +79|76 +79|47 +79|32 +76|51 +76|27 +76|44 +76|35 +76|54 +76|58 +76|61 +76|71 +19|65 +19|23 +19|46 +19|15 +19|27 +19|74 +19|44 +11|61 +11|15 +11|77 +11|71 +11|54 +11|46 +92|47 +92|22 +92|79 +92|45 +92|65 +23|46 +23|22 +23|69 +23|31 +37|95 +37|53 +37|71 +55|95 +55|14 +27|31 + +61,58,51,32,12,14,71 +58,25,54,14,12,94,32,76,39 +35,53,26,77,14,71,25,76,85,55,51,49,95 +32,91,76,11,35,61,51,77,52,19,71,84,53,58,25,44,54,94,23,12,46,69,24 +18,37,26,47,83,85,49,32,76,35,61,77,52,19,71 +39,19,84,53,58,25,54,12,69,66,31 +61,52,71,84,44,66,92 +71,84,44,46,45 +32,91,76,35,51,77,52,71,84,53,58,25,44,54,94,23,12 +48,18,37,34,26,55,47,95,83,49,32,91,11,35,61,77,52,39,71 +25,54,12,46,79,74,18 +94,65,44,39,46,66,52,15,54,71,84,92,12,77,23,27,31,16,25,58,19,69,53 +19,71,53,25,44,54,94,23,46,69,24,15,66,16,65,31,79 +49,76,11,35,77,52,39,71,54,23,12,46,69 +69,58,19,71,35,61,16,46,54 +54,16,35,94,25,15,51 +34,26,55,47,95,14,85,49,32,91,76,11,35,61,51,77,52,39,19,71,84,53,58 +76,54,77,24,46,12,27,35,53,52,61,39,58,69,94,71,91,23,51 +14,27,83,85,37,65,16,18,79,22,48,66,86,92,47 +19,52,61,58,44,84,51 +27,15,66,16,92,31,98,79,74,86,18,37,26,55,85 +94,46,27,66,65,45,98,74,86,48,18,37,34 +66,46,53,52,77,35,16,27,61 +18,37,34,55,47,95,83,14,85,49,32,91,76,11,35,61,51,77,39,71,84 +61,51,77,52,39,19,71,84,53,58,25,54,94,23,12,46,69,24,27,15,66,16,92 +98,74,86,22,48,18,37,34,26,55,47,95,83,14,85,49,32,91,76,11,51 +49,32,51,39,84,53,69 +46,15,66,65,45,98,86,48,18,34,55,47,95 +25,94,23,12,66,16,92,31,79,86,22,48,18 +32,91,76,51,77,52,39,19,53,58,25,54,94,23,12,69,24 +34,26,55,47,83,14,76,35,61,51,77,39,19,84,53 +34,18,95,74,35,37,47,52,76,32,22,48,51,86,77,85,11,55,83,14,26 +25,44,54,23,46,69,27,16,92,31,45,98,22,48,18 +91,58,11,76,26,49,19,95,14,61,39 +48,18,37,34,55,47,95,14,85,49,32,91,76,35,61,51,77,52,39,19,71 +31,66,27,79,23,86,22,92,15,44,54,94,16,37,12,18,45 +16,24,66,53,12,77,19,65,15,51,27,94,23,52,39,46,58,69,92,84,71,54,25 +44,54,94,12,46,24,27,15,92,31,86,22,37 +39,71,53,54,15,45,98 +69,53,44,54,77,58,27,39,61,25,12,94,23,51,35,84,71,15,46,66,16,19,52 +94,46,69,31,66,84,86,58,98,16,24 +55,47,95,49,76,51,19,71,84,58,44 +12,46,69,16,92,65,31,45,98,79,74,86,22,48,18,37,34,26,47 +52,11,77,58,27,35,39,24,44,19,46,23,54,53,71,76,12 +71,66,94,15,84,46,61,52,11,19,53,23,51,69,27,54,58,12,39,44,24 +79,86,22,18,37,47,83,14,91,76,51 +79,48,47,95,11 +12,24,46,34,66,74,86,98,69,37,45,92,26,55,47,18,65,22,16 +71,65,54,15,39,69,98,23,46,58,94,24,45 +79,74,86,83,14 +35,19,84,69,27 +54,46,66,31,98,48,18,37,34 +18,65,34,14,55,66,24 +94,46,27,65,98,22,26 +66,69,45,46,79,23,86,92,58,94,84,53,24,31,44,54,16,12,65,27,98 +27,66,92,65,31,98,86,18,26,55,95,83,85 +86,34,79,83,66,55,22,16,32 +85,49,32,76,11,77,52,39,84,53,25,54,94,12,46 +74,47,31,22,92,18,79,37,48,14,16,85,98,83,34,45,65,95,66,27,86 +86,22,18,37,34,26,47,95,83,85,49,32,11,35,61,77,39 +79,55,95,49,91 +54,94,23,12,46,69,24,27,15,66,16,65,31,45,98,79,74,86,22,48,18,37,34 +27,53,58,92,69,79,94 +83,14,85,49,32,91,76,11,35,61,51,77,52,39,19,71,84,53,58,44,54,94,23 +23,12,46,69,24,27,15,66,16,92,65,31,98,79,74,86,22,48,18,37,34,26,55 +53,54,19,85,46,91,84,71,44,52,23,51,77,76,39,32,25,94,49,11,61 +32,35,55,22,74 +35,61,51,77,39,19,71,84,53,58,25,44,54,94,23,12,46,69,24,27,15,66,16 +95,74,11,49,85,83,45,34,32,61,37,35,14,55,18,79,26,22,98,86,91,47,48 +66,31,45,98,22,48,47,95,83,14,85 +61,51,77,52,39,19,71,84,53,58,25,54,94,23,12,46,69,27,15,16,92 +85,49,91,35,61,77,52,39,19,71,84,53,58,25,94,12,46 +47,26,79,65,34,37,74,83,98,22,45,55,18,16,66,69,15,86,95 +61,51,46,23,12,25,85,58,52,39,54,19,76,91,94,32,35 +48,37,95,85,32,91,76,51,71 +84,58,25,44,54,94,23,12,46,69,24,27,15,66,16,92,65,31,98,79,86 +77,54,14,35,49,84,76,51,11,44,94,95,52,71,19 +69,24,27,15,66,16,92,65,31,45,98,79,74,22,48,18,37,34,26,55,47,95,83 +61,71,53,44,46,24,66 +66,16,45,18,55,47,83,85,32 +69,24,27,66,98,79,86,22,48,18,37,34,26,55,47,95,83 +85,19,71,84,44,11,53,47,77,49,76,54,14,91,25 +11,83,61,77,51,35,58,94,95 +49,32,91,76,11,35,61,51,77,52,39,19,84,58,25,44,94,23,12,46,69 +25,44,54,94,23,12,24,27,15,66,16,92,65,31,79,74,86,22,18 +71,49,48,95,76,83,34,47,37 +14,49,32,91,61,77,52,19,84,58,44 +45,44,79,53,16,23,15,58,92,31,66,22,98,74,25,24,46,69,65,86,54,27,94 +11,61,52,53,58,94,66 +77,84,54,69,71,76,24,23,94,11,44,27,91,12,61,53,35,19,39,46,25 +92,65,31,45,74,86,48,18,37,34,26,55,47,95,83,14,85,91,76 +46,24,15,66,16,92,98,79,86,18,34,26,55 +11,35,61,51,77,52,39,19,71,53,58,25,44,54,94,23,12,69,24,15,66 +49,76,39,53,58 +71,84,53,58,25,44,54,94,23,12,69,24,27,15,66,16,92,65,31,45,98,79,74 +14,25,47,32,54,19,85,95,58 +53,12,25,19,15,54,52,31,44,39,69 +32,91,76,35,61,51,77,52,39,19,84,58,25,44,54,94,23,12,46,69,24 +55,83,91,49,86,22,76,37,32,14,39,47,34 +94,23,12,46,69,24,27,66,16,92,65,31,45,98,79,74,86,22,48,18,37,34,26 +55,83,49,32,35,39,19 +19,16,92,46,31,69,44,52,39,12,71,45,53,15,84,66,58 +71,84,35,53,58,11,91,61,51,83,25,14,47,77,32,76,44,55,95,52,19,49,39 +24,27,65,31,45,98,22,48,18,34,26,55,47,95,14 +46,23,58,31,98,12,92,24,45,54,71,94,19,16,53,25,39,44,69 +92,65,98,18,47,83,14,85,76 +26,32,34,83,49,91,52,85,47,11,51,76,74,35,48 +65,45,98,79,74,22,48,18,55,47,95,83,14,85,49,32,91,76,11 +47,39,26,22,49,34,76 +77,52,84,54,23,46,27,16,31 +15,66,16,92,65,31,98,74,86,48,18,37,34,55,47 +92,55,48,18,27,45,65,24,66,83,69 +22,18,37,95,85,49,32,35,61,77,19 +49,91,77,71,58,25,54,94,69 +39,51,85,77,49,19,61,18,22,37,14,34,11 +47,85,32,91,76,52,39,19,71,84,53,44,54 +76,35,61,52,39,71,53,58,25,94,12,46,24,27,15 +61,51,84,25,46,35,76,32,52,12,23,69,19,94,58,11,39,53,24,54,77,44,91 +31,45,66,49,79,95,26,98,65,48,14,55,22,74,92 +92,46,48,26,66 +25,44,94,12,65,31,79,22,18 +45,98,79,55,14,34,26,83,74,15,86,92,85,49,18,66,65,16,22 +25,32,53,58,94,23,19,54,71,11,39,77,52,84,69,76,49 +52,39,84,53,25,44,12,24,15,66,16,92,65,31,45 +52,39,19,58,44,54,94,23,12,69,24,15,66,16,92,31,45 +95,66,47,26,18,83,32,45,85,98,86,37,55 +31,45,86,48,37,26,95,83,14,85,32,76,35 +94,24,92,58,19,54,69,16,84,53,27,51,77,12,15,61,39,25,52,23,71,66,44 +44,54,94,23,12,46,69,24,66,92,31,98,79 +66,16,92,65,45,98,79,74,86,22,48,18,37,26,55,47,95,83,14,49,32 +98,18,47,14,85,49,51 +55,85,91,11,84 +32,91,11,35,51,77,52,39,19,84,58,54,94,23,69 +83,84,11,26,77,34,14,85,71,52,18 +32,49,91,76,83,94,71,19,23,61,58 +44,69,16,98,65,58,12,84,94,25,79,27,31,54,46,24,86,66,74,45,92 +65,94,15,12,45,74,23,58,16,84,86,31,69,24,98,92,66 +61,51,77,52,19,71,84,53,25,44,94,23,12,46,69,24,27,15,66 +79,34,55,61,98,45,47,74,83,49,76,11,32 +86,22,48,18,37,83,85,32,91,76,11,35,61,51,77,52,39 +65,45,86,55,95,85,32,76,11 +15,66,16,92,31,45,98,79,74,86,22,48,18,37,34,26,95,83,14,85,49 +54,58,53,16,71,15,98,69,94,65,46,31,66,25,92,84,44,24,19,23,27 +26,61,37,14,71,51,19,76,91 +15,92,79,74,86,22,48,26,55,83,14,85,49 +26,74,83,76,49,31,47,35,37,95,86 +71,83,95,34,19,39,14,51,85,49,52,61,32,48,55 +69,15,66,92,79,74,37,34,55,47,95 +47,51,19,39,35,85,84,91,26,71,49,55,25,11,52 +26,14,76,32,37,95,86,35,55,79,83,11,91,47,34,22,31,98,85,45,48 +35,53,19,32,91,46,12,25,11,61,54,51,71 +98,79,74,86,22,18,37,34,26,55,47,95,83,85,49,32,91,11,51 +79,22,34,47,32,11,77 +31,69,45,95,83,16,55,65,98,22,26,37,74 +74,86,22,37,34,26,55,95,85,32,91,76,11,77,52 +39,19,71,53,25,44,54,46,69,24,27,66,16,65,31,45,98 +47,51,11,77,58,85,52 +45,48,37,83,49,32,91 +85,26,77,95,19,18,51,34,55,52,91,32,11,84,39 +79,45,18,94,48,74,16,12,92,98,37,26,27,23,69,46,34,86,31 +48,34,14,45,86,98,18,85,47,22,55,76,79,32,31,91,35,83,37 +76,77,52,71,44 +26,55,47,95,83,14,85,49,32,91,76,11,35,61,51,77,52,39,19,71,84,58,25 +45,98,79,86,48,18,34,26,47,95,85,91,61 +24,44,19,71,15,12,27,51,52,92,84 +69,24,27,65,98,22,48,18,55,47,83 +61,34,11,74,91,51,48,55,52,86,37,85,14,49,95,76,22 +71,46,27,15,66,92,79 +58,46,16,31,27,54,53,66,12,23,71,52,84,25,44,94,77 +61,77,39,19,71,84,25,44,54,94,24,27,15,66,16 +51,52,15,12,24,66,54,35,46,16,69,84,53 +74,86,22,48,18,34,26,55,47,95,83,14,85,49,32,91,76,11,35,61,51,77,52 +34,26,47,14,32,76,35,39,71 +37,26,47,83,85,49,76,11,35,61,51,77,53 +66,24,98,86,15 +91,76,35,61,51,52,39,19,53,58,54,94,23,46,27 +16,92,45,74,86,22,18,37,34,26,55,85,49,32,91 +12,74,58,79,23,31,22,25,86 +98,79,22,48,18,14,85,49,32,76,11,61,51 +34,26,55,47,95,83,14,85,49,32,91,76,11,35,61,51,52,39,19,71,84,53,58 +51,52,39,58,25,44,54,12,46,69,24,66,16,92,65 +24,44,58,12,46,92,22,79,86,74,54,65,23 +86,22,18,26,95,32,91,76,51 +71,54,69,24,15,66,16,31,45,98,74 +15,92,65,31,45,98,79,74,86,22,48,18,34,26,55,95,14,85,49 +53,44,54,94,23,12,46,69,24,27,15,66,92,98,74,86,22 +16,92,65,31,45,98,22,18,37,34,55,95,83,14,32 +34,47,95,83,14,85,49,91,76,11,35,51,77,52,19,71,84,53,58 +55,45,76,37,35,83,31 diff --git a/package.yaml b/package.yaml index 33b9d07..c0883bd 100644 --- a/package.yaml +++ b/package.yaml @@ -22,6 +22,8 @@ description: Please see the README on GitHub at = 4.7 && < 5 - regex-tdfa ^>= 1.3.2 + - text >= 2.0.2 + - containers ghc-options: - -Wall diff --git a/src/Day5.hs b/src/Day5.hs new file mode 100644 index 0000000..0d7a5d7 --- /dev/null +++ b/src/Day5.hs @@ -0,0 +1,55 @@ +module Day5 (ParseError, partOne, partTwo) where + +import qualified Data.Bifunctor as BF +import Data.List (sortBy) +import Data.Set (Set, fromList, member) +import qualified Data.Text as T +import Data.Text.Read (decimal) + +data ParseError = ParseIntError String | ParseRuleError T.Text | ParseInputError + deriving (Show, Eq) + +type Rule = (Int, Int) + +type Update = [Int] + +partOne :: String -> Either ParseError Int +partOne input = do + (rules, updates) <- parseInput input + return $ sum $ map middleElem $ filter (isValidUpdate rules) updates + where + isValidUpdate rules update = sortBy (compareByRules rules) update == update + compareByRules rules x y = if member (x, y) rules then LT else GT + middleElem update = update !! (length update `div` 2) + +partTwo :: String -> Either ParseError Int +partTwo _ = Right 0 + +parseInput :: String -> Either ParseError (Set Rule, [Update]) +parseInput content = case T.splitOn separator text of + [left, right] -> do + rules <- parseRules left + updates <- parseUpdates right + return (rules, updates) + _ -> Left ParseInputError + where + text = T.pack content + separator = T.pack "\n\n" + +parseRules :: T.Text -> Either ParseError (Set Rule) +parseRules rules = fromList <$> mapM parseRule (T.lines rules) + where + parseRule rule = case T.split (== '|') rule of + [left, right] -> do + leftNum <- parseInt left + rightNum <- parseInt right + return (leftNum, rightNum) + _ -> Left (ParseRuleError rule) + +parseUpdates :: T.Text -> Either ParseError [Update] +parseUpdates updates = mapM parseUpdate $ T.lines updates + where + parseUpdate = mapM parseInt . T.split (== ',') + +parseInt :: T.Text -> Either ParseError Int +parseInt v = fst <$> BF.first ParseIntError (decimal v) diff --git a/test/Day5Spec.hs b/test/Day5Spec.hs new file mode 100644 index 0000000..14eb2b1 --- /dev/null +++ b/test/Day5Spec.hs @@ -0,0 +1,40 @@ +module Day5Spec (spec) where + +import Day5 (partOne) +import Test.Hspec + +spec :: Spec +spec = do + describe "PartOne" $ do + it "works" $ do + partOne input `shouldBe` Right 143 + where + input = + "47|53\n\ + \97|13\n\ + \97|61\n\ + \97|47\n\ + \75|29\n\ + \61|13\n\ + \75|53\n\ + \29|13\n\ + \97|29\n\ + \53|29\n\ + \61|53\n\ + \97|53\n\ + \61|29\n\ + \47|13\n\ + \75|47\n\ + \97|75\n\ + \47|61\n\ + \75|61\n\ + \47|29\n\ + \75|13\n\ + \53|13\n\ + \\n\ + \75,47,61,53,29\n\ + \97,61,53,29,13\n\ + \75,29,13\n\ + \75,97,47,61,53\n\ + \61,13,29\n\ + \97,13,75,29,47"