aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2024/05-Print_Queue/example28
-rw-r--r--2024/05-Print_Queue/first.hs55
-rw-r--r--2024/05-Print_Queue/input1386
-rw-r--r--2024/05-Print_Queue/second.hs57
4 files changed, 1526 insertions, 0 deletions
diff --git a/2024/05-Print_Queue/example b/2024/05-Print_Queue/example
new file mode 100644
index 0000000..9d146d6
--- /dev/null
+++ b/2024/05-Print_Queue/example
@@ -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/2024/05-Print_Queue/first.hs b/2024/05-Print_Queue/first.hs
new file mode 100644
index 0000000..c2fa003
--- /dev/null
+++ b/2024/05-Print_Queue/first.hs
@@ -0,0 +1,55 @@
+-- requires cabal install --lib megaparsec parser-combinators heap vector
+module Main (main) where
+
+import Control.Monad (void, when)
+import qualified Data.List as L
+import Data.Void (Void)
+import Text.Megaparsec
+import Text.Megaparsec.Char
+
+exampleExpectedOutput = 143
+
+type Rule = (Int, Int)
+type Update = [Int]
+data Input = Input [Rule] [Update] deriving Show
+
+type Parser = Parsec Void String
+
+parseNumber :: Parser Int
+parseNumber = read <$> some digitChar
+
+parseRule :: Parser Rule
+parseRule = (,) <$> parseNumber <* char '|'
+ <*> parseNumber <* eol
+
+parseUpdate :: Parser Update
+parseUpdate = some (parseNumber <* optional (char ',')) <* eol
+
+parseInput' :: Parser Input
+parseInput' = Input <$> some parseRule <* eol
+ <*> some parseUpdate <* eof
+
+parseInput :: String -> IO Input
+parseInput filename = do
+ input <- readFile filename
+ case runParser parseInput' filename input of
+ Left bundle -> error $ errorBundlePretty bundle
+ Right input' -> return input'
+
+compute :: Input -> Int
+compute (Input rules updates) = L.foldl' compute' 0 updates
+ where
+ compute' acc update | valid update = acc + update !! (length update `div` 2)
+ | otherwise = acc
+ valid update = all (valid' update) rules
+ valid' update (x, y) = case (L.elemIndices x update, L.elemIndices y update) of
+ ([i], [j]) -> i < j
+ _ -> True
+
+main :: IO ()
+main = do
+ example <- parseInput "example"
+ let exampleOutput = compute example
+ when (exampleOutput /= exampleExpectedOutput) (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
+ input <- parseInput "input"
+ print $ compute input
diff --git a/2024/05-Print_Queue/input b/2024/05-Print_Queue/input
new file mode 100644
index 0000000..167a0c2
--- /dev/null
+++ b/2024/05-Print_Queue/input
@@ -0,0 +1,1386 @@
+23|96
+53|45
+53|92
+73|27
+73|37
+73|24
+31|84
+31|76
+31|59
+31|73
+55|29
+55|31
+55|73
+55|68
+55|57
+68|78
+68|59
+68|13
+68|56
+68|24
+68|48
+52|61
+52|68
+52|55
+52|92
+52|97
+52|11
+52|57
+37|31
+37|76
+37|59
+37|41
+37|11
+37|55
+37|71
+37|57
+71|85
+71|42
+71|18
+71|48
+71|98
+71|13
+71|38
+71|63
+71|88
+97|13
+97|76
+97|46
+97|71
+97|57
+97|11
+97|12
+97|39
+97|54
+97|59
+98|77
+98|26
+98|78
+98|23
+98|42
+98|24
+98|17
+98|51
+98|18
+98|73
+98|53
+46|85
+46|76
+46|84
+46|59
+46|14
+46|68
+46|45
+46|92
+46|12
+46|83
+46|54
+46|71
+92|13
+92|41
+92|54
+92|55
+92|71
+92|85
+92|84
+92|65
+92|31
+92|94
+92|11
+92|83
+92|82
+14|41
+14|29
+14|12
+14|61
+14|84
+14|39
+14|82
+14|98
+14|68
+14|11
+14|31
+14|83
+14|85
+14|55
+84|13
+84|74
+84|83
+84|73
+84|41
+84|98
+84|56
+84|71
+84|78
+84|39
+84|77
+84|94
+84|29
+84|63
+84|85
+57|85
+57|59
+57|12
+57|73
+57|61
+57|13
+57|56
+57|83
+57|39
+57|71
+57|41
+57|84
+57|48
+57|51
+57|54
+57|76
+45|83
+45|12
+45|29
+45|73
+45|71
+45|84
+45|94
+45|56
+45|11
+45|82
+45|57
+45|61
+45|68
+45|31
+45|39
+45|55
+45|48
+39|71
+39|29
+39|88
+39|13
+39|23
+39|83
+39|24
+39|77
+39|18
+39|94
+39|51
+39|85
+39|52
+39|98
+39|74
+39|38
+39|63
+39|42
+48|65
+48|27
+48|26
+48|46
+48|63
+48|52
+48|62
+48|97
+48|18
+48|38
+48|51
+48|14
+48|77
+48|23
+48|74
+48|78
+48|88
+48|37
+48|53
+77|53
+77|62
+77|26
+77|97
+77|65
+77|68
+77|63
+77|46
+77|92
+77|55
+77|57
+77|17
+77|31
+77|37
+77|61
+77|38
+77|11
+77|14
+77|12
+77|18
+18|92
+18|26
+18|62
+18|61
+18|59
+18|45
+18|53
+18|65
+18|41
+18|97
+18|55
+18|57
+18|84
+18|31
+18|46
+18|17
+18|11
+18|54
+18|12
+18|82
+18|37
+29|24
+29|88
+29|23
+29|56
+29|62
+29|83
+29|98
+29|63
+29|74
+29|85
+29|13
+29|18
+29|94
+29|51
+29|27
+29|78
+29|42
+29|17
+29|73
+29|52
+29|96
+29|48
+78|37
+78|62
+78|45
+78|26
+78|17
+78|96
+78|55
+78|77
+78|65
+78|46
+78|27
+78|57
+78|97
+78|38
+78|18
+78|42
+78|52
+78|53
+78|88
+78|11
+78|92
+78|74
+78|14
+62|39
+62|46
+62|76
+62|26
+62|82
+62|92
+62|57
+62|45
+62|61
+62|17
+62|59
+62|14
+62|55
+62|12
+62|84
+62|53
+62|68
+62|97
+62|31
+62|11
+62|65
+62|41
+62|54
+62|37
+24|37
+24|27
+24|62
+24|46
+24|23
+24|51
+24|96
+24|92
+24|18
+24|38
+24|26
+24|45
+24|53
+24|88
+24|65
+24|97
+24|17
+24|52
+24|74
+24|42
+24|63
+24|78
+24|77
+24|14
+85|63
+85|97
+85|62
+85|78
+85|48
+85|74
+85|77
+85|37
+85|94
+85|88
+85|73
+85|26
+85|17
+85|23
+85|96
+85|27
+85|24
+85|98
+85|51
+85|38
+85|18
+85|56
+85|42
+85|52
+61|76
+61|12
+61|78
+61|71
+61|23
+61|24
+61|85
+61|83
+61|13
+61|59
+61|94
+61|41
+61|98
+61|73
+61|74
+61|54
+61|56
+61|48
+61|51
+61|82
+61|84
+61|96
+61|39
+61|29
+26|57
+26|84
+26|65
+26|53
+26|76
+26|82
+26|68
+26|59
+26|41
+26|14
+26|61
+26|13
+26|83
+26|92
+26|71
+26|46
+26|54
+26|29
+26|11
+26|55
+26|31
+26|39
+26|45
+26|12
+13|62
+13|98
+13|18
+13|94
+13|38
+13|96
+13|23
+13|52
+13|77
+13|24
+13|88
+13|85
+13|37
+13|78
+13|63
+13|56
+13|42
+13|74
+13|27
+13|17
+13|83
+13|73
+13|51
+13|48
+56|92
+56|96
+56|48
+56|97
+56|88
+56|51
+56|42
+56|74
+56|27
+56|46
+56|77
+56|14
+56|23
+56|62
+56|26
+56|78
+56|63
+56|24
+56|53
+56|37
+56|18
+56|17
+56|38
+56|52
+12|78
+12|23
+12|76
+12|54
+12|41
+12|51
+12|56
+12|42
+12|29
+12|13
+12|84
+12|52
+12|98
+12|48
+12|82
+12|73
+12|83
+12|85
+12|24
+12|71
+12|39
+12|74
+12|96
+12|94
+41|42
+41|88
+41|38
+41|24
+41|52
+41|63
+41|73
+41|27
+41|48
+41|78
+41|98
+41|74
+41|51
+41|83
+41|96
+41|85
+41|94
+41|39
+41|56
+41|23
+41|77
+41|71
+41|29
+41|13
+59|98
+59|82
+59|73
+59|84
+59|85
+59|29
+59|56
+59|52
+59|41
+59|54
+59|39
+59|83
+59|96
+59|23
+59|94
+59|24
+59|78
+59|48
+59|71
+59|74
+59|51
+59|76
+59|13
+59|12
+94|62
+94|46
+94|37
+94|88
+94|27
+94|51
+94|42
+94|56
+94|78
+94|73
+94|52
+94|96
+94|24
+94|18
+94|77
+94|74
+94|48
+94|26
+94|23
+94|17
+94|98
+94|63
+94|38
+94|97
+96|37
+96|57
+96|52
+96|65
+96|26
+96|17
+96|53
+96|63
+96|11
+96|14
+96|46
+96|55
+96|88
+96|45
+96|77
+96|68
+96|92
+96|42
+96|97
+96|18
+96|62
+96|31
+96|27
+96|38
+38|59
+38|97
+38|17
+38|82
+38|46
+38|57
+38|92
+38|31
+38|37
+38|62
+38|45
+38|61
+38|84
+38|55
+38|26
+38|53
+38|65
+38|68
+38|76
+38|11
+38|18
+38|54
+38|14
+38|12
+88|17
+88|92
+88|26
+88|76
+88|12
+88|18
+88|61
+88|31
+88|54
+88|63
+88|65
+88|57
+88|14
+88|97
+88|45
+88|37
+88|68
+88|62
+88|11
+88|53
+88|59
+88|38
+88|55
+88|46
+82|24
+82|29
+82|94
+82|77
+82|88
+82|83
+82|48
+82|52
+82|56
+82|73
+82|78
+82|39
+82|96
+82|42
+82|41
+82|27
+82|98
+82|13
+82|84
+82|74
+82|23
+82|51
+82|85
+82|71
+51|18
+51|45
+51|14
+51|92
+51|27
+51|17
+51|88
+51|38
+51|46
+51|63
+51|11
+51|26
+51|97
+51|53
+51|55
+51|74
+51|62
+51|42
+51|52
+51|37
+51|96
+51|78
+51|65
+51|77
+74|77
+74|31
+74|26
+74|11
+74|45
+74|65
+74|17
+74|55
+74|42
+74|18
+74|57
+74|38
+74|14
+74|97
+74|52
+74|63
+74|96
+74|62
+74|37
+74|88
+74|27
+74|53
+74|46
+74|92
+83|74
+83|27
+83|96
+83|24
+83|63
+83|94
+83|38
+83|37
+83|18
+83|23
+83|42
+83|62
+83|52
+83|98
+83|56
+83|17
+83|78
+83|88
+83|51
+83|97
+83|85
+83|48
+83|73
+83|77
+17|71
+17|31
+17|26
+17|11
+17|53
+17|84
+17|37
+17|82
+17|14
+17|41
+17|45
+17|57
+17|55
+17|76
+17|54
+17|39
+17|68
+17|12
+17|46
+17|92
+17|97
+17|65
+17|59
+17|61
+11|41
+11|68
+11|24
+11|56
+11|23
+11|48
+11|61
+11|29
+11|59
+11|82
+11|84
+11|85
+11|12
+11|76
+11|83
+11|98
+11|13
+11|57
+11|73
+11|71
+11|31
+11|39
+11|54
+11|94
+65|71
+65|61
+65|85
+65|98
+65|41
+65|68
+65|45
+65|13
+65|84
+65|54
+65|56
+65|59
+65|11
+65|83
+65|57
+65|39
+65|29
+65|73
+65|31
+65|94
+65|76
+65|55
+65|12
+65|82
+42|27
+42|77
+42|61
+42|59
+42|46
+42|45
+42|55
+42|97
+42|92
+42|26
+42|63
+42|14
+42|68
+42|57
+42|31
+42|38
+42|65
+42|18
+42|37
+42|88
+42|11
+42|17
+42|53
+42|62
+54|29
+54|94
+54|78
+54|56
+54|83
+54|98
+54|77
+54|52
+54|24
+54|27
+54|85
+54|73
+54|74
+54|96
+54|41
+54|82
+54|48
+54|84
+54|51
+54|71
+54|13
+54|39
+54|23
+54|42
+27|46
+27|92
+27|77
+27|53
+27|65
+27|97
+27|18
+27|14
+27|17
+27|31
+27|37
+27|63
+27|55
+27|45
+27|61
+27|57
+27|59
+27|38
+27|62
+27|88
+27|68
+27|12
+27|26
+27|11
+76|78
+76|23
+76|39
+76|96
+76|82
+76|85
+76|84
+76|27
+76|51
+76|74
+76|41
+76|83
+76|13
+76|54
+76|52
+76|73
+76|24
+76|42
+76|56
+76|71
+76|94
+76|48
+76|29
+76|98
+63|37
+63|31
+63|62
+63|68
+63|12
+63|76
+63|45
+63|61
+63|82
+63|59
+63|54
+63|53
+63|17
+63|18
+63|11
+63|65
+63|92
+63|57
+63|97
+63|55
+63|14
+63|26
+63|46
+63|38
+23|74
+23|37
+23|97
+23|55
+23|78
+23|18
+23|92
+23|45
+23|77
+23|27
+23|26
+23|38
+23|46
+23|88
+23|14
+23|51
+23|52
+23|53
+23|17
+23|42
+23|63
+23|62
+23|65
+53|31
+53|94
+53|83
+53|41
+53|85
+53|65
+53|29
+53|14
+53|55
+53|13
+53|61
+53|57
+53|82
+53|59
+53|84
+53|76
+53|11
+53|71
+53|68
+53|39
+53|12
+53|54
+73|51
+73|77
+73|23
+73|97
+73|38
+73|14
+73|74
+73|46
+73|56
+73|48
+73|96
+73|26
+73|63
+73|18
+73|52
+73|62
+73|42
+73|17
+73|78
+73|53
+73|88
+31|48
+31|78
+31|83
+31|85
+31|12
+31|39
+31|56
+31|71
+31|13
+31|54
+31|68
+31|29
+31|82
+31|61
+31|41
+31|23
+31|98
+31|94
+31|24
+31|51
+55|41
+55|56
+55|13
+55|39
+55|84
+55|94
+55|54
+55|76
+55|61
+55|85
+55|11
+55|59
+55|12
+55|71
+55|48
+55|83
+55|82
+55|98
+55|24
+68|76
+68|51
+68|54
+68|23
+68|84
+68|74
+68|61
+68|85
+68|94
+68|39
+68|82
+68|71
+68|98
+68|41
+68|83
+68|29
+68|73
+68|12
+52|37
+52|53
+52|63
+52|88
+52|65
+52|17
+52|46
+52|31
+52|26
+52|14
+52|77
+52|45
+52|27
+52|62
+52|38
+52|42
+52|18
+37|45
+37|97
+37|61
+37|29
+37|39
+37|92
+37|54
+37|84
+37|14
+37|65
+37|53
+37|46
+37|82
+37|26
+37|68
+37|12
+71|74
+71|51
+71|78
+71|52
+71|83
+71|56
+71|27
+71|29
+71|94
+71|73
+71|24
+71|96
+71|62
+71|77
+71|23
+97|68
+97|55
+97|41
+97|14
+97|45
+97|53
+97|26
+97|92
+97|31
+97|29
+97|84
+97|65
+97|61
+97|82
+98|52
+98|63
+98|62
+98|27
+98|74
+98|38
+98|96
+98|37
+98|46
+98|48
+98|56
+98|88
+98|97
+46|65
+46|61
+46|39
+46|57
+46|53
+46|55
+46|41
+46|29
+46|82
+46|11
+46|13
+46|31
+92|39
+92|12
+92|29
+92|68
+92|45
+92|73
+92|76
+92|98
+92|57
+92|59
+92|61
+14|57
+14|45
+14|65
+14|71
+14|76
+14|92
+14|59
+14|13
+14|54
+14|94
+84|48
+84|24
+84|51
+84|52
+84|96
+84|27
+84|42
+84|23
+84|88
+57|68
+57|23
+57|94
+57|29
+57|98
+57|24
+57|82
+57|31
+45|85
+45|98
+45|76
+45|13
+45|41
+45|54
+45|59
+39|73
+39|27
+39|96
+39|78
+39|56
+39|48
+48|17
+48|96
+48|24
+48|42
+48|92
+77|45
+77|59
+77|76
+77|88
+18|76
+18|14
+18|68
+29|77
+29|38
+78|63
+
+88,63,38,62,17,37,97,26,46,53,14,92,65,45,55,11,57,31,68,61,59,12,76
+46,53,14,92,65,55,11,57,31,59,12,54,82,84,41,39,29,13,83
+13,83,94,73,48,74,96,77,38,62,17
+31,76,54,41,29,48,51
+52,42,27,77,88,63,38,18,37,46,14,92,65,55,11,31,68
+52,98,29,56,48,96,84,24,83,76,42,74,85
+13,83,85,94,73,48,24,78,96,52,77,63,38
+42,27,77,88,18,62,17,37,97,26,46,53,14,65,45,55,11,57,31,68,61
+57,97,76,71,84,37,12
+57,31,68,59,12,76,54,84,41,39,71,29,13,83,85,94,98,73,48,24,23
+82,29,83,68,39,92,41,61,85
+29,39,41,56,52,63,83,71,77,94,74,78,24,98,96
+48,24,51,78,96,52,42,27,77,88,63,18,62,97,26,53,14
+85,42,27,88,62,37,97
+46,53,14,92,65,45,55,11,57,31,68,61,59,12,76,54,82,84,41,39,29,13,83
+73,23,51,78,74,27,88,18,37,26,53
+55,38,37,62,61,17,57,76,12
+68,65,46,11,31,12,92,37,59,14,76,55,61,18,63,45,17,62,54,38,97,57,26
+29,85,94,51,42,63,62
+62,17,11,31,61
+88,17,14,63,97
+73,56,48,24,23,51,78,74,96,52,42,27,77,88,63,38,18,62,17,37,97,46,53
+11,71,82,68,85,61,12,48,56,41,73,13,24,31,76,29,94
+84,71,13,48,52,42,88
+12,76,54,84,39,71,29,83,85,98,73,48,24,51,78,74,96
+63,18,17,37,65,11,31,68,54
+96,77,38,17,46,45,55,57,31
+59,54,65,55,97,38,37,57,92,12,82
+77,88,38,37,97,46,53,92,45,11,61,59,12
+78,74,96,52,42,27,77,88,38,18,62,46,53,14,92,65,45,55,11
+62,17,37,97,26,14,92,65,45,55,11,57,31,68,61,59,76,54,82,84,41
+57,98,23,56,68,48,76,59,94
+88,37,65,45,31,68,76
+45,11,31,68,12,29,83
+77,38,17,78,62,63,51,27,45,23,92,14,88
+54,97,14,68,12,17,82,38,37,46,57
+41,29,83,94,48,23,51,78,74,96,52,27,77,88,63
+26,37,62,55,77
+98,73,56,48,24,23,51,78,74,96,42,27,77,88,63,38,18,62,17,37,97,26,46
+11,37,26,92,55,53,59,88,57,65,68,38,46,17,31,45,27,61,97
+84,39,71,29,13,83,85,94,73,56,48,51,78,74,96,52,42,77,88
+12,51,41,61,13,98,39,29,82,76,71,84,68,85,59,23,94,73,48,78,56,83,54
+51,42,74,37,78,26,38,18,52,17,14,53,77
+12,76,54,82,41,39,71,29,13,83,85,94,98,73,56,48,24,23,51,78,74,96,52
+77,38,18,62,17,37,46,53,92,45,55,11,57,61,12
+14,31,12,39,13
+18,85,51,52,56,38,27,13,83,73,88,78,77,42,74,62,24,96,29
+11,92,55,31,63,62,59,77,45
+54,62,12,55,65,37,41,57,31,26,97,46,11
+27,56,77,62,48,88,37,38,46,73,42,52,18,53,17
+61,12,76,54,84,41,39,29,83,94,98,48,24,23,51,78,74
+24,56,52,63,18
+26,46,53,14,92,65,45,55,59,76,82,84,41,39,13
+45,29,85,61,12,41,94,68,55,65,31,82,71,84,39,83,59,11,13,73,57,98,54
+98,73,56,48,24,23,51,78,74,52,42,27,77,88,18,62,17,37,97,26,46
+41,74,23,51,24,13,84,48,82,56,52,85,78
+46,53,92,65,45,55,11,57,31,68,12,76,54,84,41,39,71,13,83
+11,41,53,55,57
+78,74,52,88,63,18,37,55,11
+62,46,53,45,11,31,76
+57,59,54,71,29,85,94,24,23
+57,31,68,61,12,76,54,82,84,41,39,71,83,85,94,73,48,24,23
+17,97,46,57,68,76,39
+39,29,83,94,98,73,56,48,24,78,74,96,52,42,27,63,38
+73,48,24,23,51,42,63,18,62,37,26,46,53
+57,31,68,61,12,76,54,82,84,41,39,71,29,85,94,98,56,48,24
+46,53,14,55,31,68,59,76,54,82,84,13,83
+54,59,83,78,96,84,82,48,94
+65,17,46,14,45,92,88,55,57,27,31,53,42,97,77,61,18
+85,83,27,52,71,77,78,96,18,51,38
+24,61,59,54,82,68,85,83,11,12,71,76,84
+41,83,48,51,78,74,52,42,63
+65,45,11,57,31,68,12,76,82,41,39,13,94,98,73
+54,27,51,84,83,39,23,96,48
+83,98,55,13,71,56,76,11,84,31,73,59,29,48,85,61,12,54,68,39,82,41,94
+31,68,61,54,39,71,29,13,83,85,23
+65,45,31,61,59,68,26,37,46,77,88,27,11
+54,82,84,41,39,71,83,85,98,73,24,23,78,74,96,42,27
+45,55,11,57,31,68,59,12,76,54,82,84,41,39,71,29,13,83,85,94,98,73,56
+11,62,17,42,53,88,52,18,96,97,74,63,38,92,14,26,78
+37,54,12,61,68,92,62,18,57,59,26,46,97,84,14,82,65
+92,65,55,61,76
+37,26,62,42,92,96,78,51,97,48,38,18,24
+59,12,76,54,82,84,41,39,71,29,13,83,85,94,98,73,48,24,23,51,78,74,96
+88,63,38,18,17,97,53,14,92,65,55,57,68,12,76
+98,68,59,61,11,73,13,71,39,56,94,41,12,29,57,84,54,55,85,45,82
+68,59,12,76,54,39,71,83,73,56,48
+52,56,63,13,98,94,17,18,23
+62,98,51,29,38,13,83,18,42
+76,54,82,41,39,29,13,83,85,94,56,51,78,96,52
+12,76,54,82,84,41,39,71,29,13,83,85,94,73,48,24,23,51,78,74,96
+83,29,74,78,56,85,13,62,23
+85,56,48,51,52,42,27,77,63,38,17,37,97
+83,51,77,17,38,48,63,78,73,13,42,18,27,52,56,94,24,96,85,98,74
+74,96,42,27,77,88,63,18,17,37,97,26,46,53,14,92,65,45,55,11,57
+68,61,59,12,76,54,82,84,39,71,29,13,83,85,94,98,73,56,48,24,23,51,78
+42,46,53,23,18,92,62,27,38,37,77,48,97
+18,45,68,12,76
+84,61,13,55,29,76,65,54,82,31,73,41,71,45,68,39,57,11,98,59,85
+77,88,63,38,62,17,37,97,26,46,53,14,92,65,45,55,11,57,31,68,61,59,12
+37,97,46,53,14,92,45,55,11,57,31,68,59,12,76,54,82,84,41,39,71
+24,23,51,78,96,52,42,27,77,63,38,18,62,17,37,97,26,46,53,92,65
+12,45,11,73,55,61,29,71,56,76,54,82,98,57,59,83,94,13,31,84,85,68,39
+53,14,92,65,45,55,11,57,31,68,61,59,12,76,54,82,84,41,39,71,13,83,85
+85,98,73,48,24,51,78,74,96,52,42,27,77,88,38,18,62,17,97
+78,74,52,27,88,63,38,37,97,26,46,53,14,92,65,45,55
+42,77,63,18,17,37,97,26,46,53,92,65,45,55,11,57,31,68,61
+61,82,84,29,13,83,85,98,48,24,23
+83,85,94,98,73,56,48,24,23,51,78,74,96,52,42,27,77,88,38,18,62,17,37
+92,65,45,55,11,57,31,68,61,59,12,76,54,41,39,71,13,83,85,94,98
+68,41,61,55,54,71,37,92,26
+98,73,56,24,96,52,38,62,37
+92,29,13,26,11,57,71
+42,23,78,38,63,77,52,62,92,18,26,45,88,17,14,65,96,27,51,53,46
+37,26,46,53,14,92,65,45,55,11,31,68,61,12,76,54,82,84,41,39,71
+42,27,77,88,38,18,17,37,97,26,46,53,14,92,65,45,55,57,68
+48,39,57,55,84,68,85,59,94,98,83,71,61,13,76
+24,23,51,78,74,96,52,27,63,38,62,37,97,26,46,53,14,92,65
+45,55,11,31,68,61,59,12,54,82,41,39,71,13,83,85,98,73,56
+62,37,97,26,46,14,92,45,11,31,68,61,12,82,41
+97,74,52,63,62,38,65,24,92,78,37,18,46
+27,51,82,41,39,24,73,56,23,83,77,13,78
+98,84,83,29,48,56,13,59,73,24,61,12,57,82,23,76,94,85,54
+38,18,62,17,37,26,53,92,65,45,55,11,57,31,61,59,12,76,82
+74,18,38,96,48,85,77,23,83,98,62,52,88,78,56
+27,52,13,98,78,42,88,23,51,73,74,39,96,77,94,84,56
+74,38,18,62,56,51,26,97,48,23,77,17,78
+82,39,55,68,31,65,97,14,53,41,46,54,17
+52,27,77,18,62,17,37,97,14,45,31
+76,84,71,83,85,94,98,24,42
+42,27,77,88,63,38,18,62,17,97,46,53,14,92,65,45,55,11,57,31,61
+11,57,31,12,76,71,13,48,24
+61,59,12,76,54,84,41,39,71,29,13,83,85,94,98,56,24,51,74
+45,62,76,59,11,65,26,61,57,18,55,14,38,68,97,46,12,31,17,63,88,37,53
+38,18,62,17,37,97,26,46,53,14,92,45,55,31,59,12,76,54,82
+71,54,59,57,41,84,39,14,82,46,61,76,13,55,11
+57,61,84,41,85,94,73,48,23
+68,59,12,41,39,29,94,98,73,51,78
+31,68,61,59,12,76,54,82,84,41,39,71,29,13,85,94,98,73,56,48,24,23,51
+48,74,63,18,46
+78,52,53,97,96,27,88,46,37,92,63,17,38,11,77,74,62
+45,11,57,31,68,61,59,12,76,54,82,84,41,39,71,29,13,94,98,73,56
+85,94,56,48,23,51,78,96,52,42,27,63,38,62,97
+57,92,55,26,11,39,54,65,14,37,45
+94,98,73,56,24,23,51,78,74,96,52,27,88,63,38,18,62,97,26
+26,24,96,27,51,17,18,97,42
+17,37,53,92,55,31,68,84,39
+41,39,71,29,13,85,98,48,23,51,78,74,52,42,27,88,63
+51,41,94,61,48,54,76,71,31,56,13,39,98
+71,29,13,85,94,98,73,24,23,51,78,74,96,52,42,27,77,88,63,38,18
+56,48,23,74,96,63,17
+71,98,54,31,65,73,83,29,13,61,82
+18,31,65,62,61,37,68,82,46,76,97,11,84,92,14
+73,56,24,23,78,96,42,27,88,63,38,18,17,37,46
+84,39,83,31,94,71,13,51,76,85,23,59,29,54,12,68,48
+94,56,48,74,52,42,27,18,17,37,26
+31,29,76,59,53,82,39,71,83,54,61,84,85,55,65
+51,39,41,84,78,23,71,76,73,13,96
+82,96,23,48,59,56,51
+52,27,77,88,38,18,62,17,37,97,46,14,65,45,55,11,57,31,68
+18,98,73,97,24,56,62,27,48,46,77,96,42,38,51,74,17,37,63,23,52,26,78
+39,71,29,13,85,94,56,48,24,23,51,78,74,52,42,27,77,88,38
+74,27,96,57,92,38,17
+39,71,13,83,85,94,56,48,23,51,96,52,42,27,88,63,38
+76,54,84,41,39,71,13,83,24,51,78,74,42
+52,78,62,17,11,55,97
+55,92,76,84,82,13,85
+82,84,71,13,85,56,74,42,77
+27,59,11,17,53,26,55,14,57,61,45,18,38,88,62,37,63,97,68
+59,61,39,37,41,17,11,26,97,14,76,84,46,57,55,92,68,31,45,12,65
+77,26,94,52,17,18,97,56,96
+71,13,94,73,56,78,96,52,42,88,18
+73,83,71,51,98,56,29,96,77,63,24,85,38,94,18,27,74
+51,74,96,27,88,63,46,14,55
+92,62,76,65,53,12,61,45,41
+39,71,29,13,83,85,94,73,56,48,24,23,51,78,74,96,52,42,27,77,88,63,38
+83,85,94,98,73,56,48,24,23,51,78,74,96,52,42,27,88,63,38,18,62
+85,94,98,73,56,48,24,23,51,78,96,52,42,27,88,63,38,62,17,37,97
+94,98,73,56,48,24,23,78,74,52,77,88,63,38,18,62,37
+78,74,96,52,42,77,63,18,17,97,14,92,65,55,11
+24,78,77,63,18,62,17,53,14,92,65
+85,94,23,51,78,96,52,27,38,17,97
+77,88,63,38,18,62,17,37,97,26,46,53,14,92,65,55,11,57,31,68,61,59,12
+51,98,83,23,84,59,61,82,76,71,73,78,56,13,41,54,39,48,24,12,94,85,68
+94,78,62,88,96,29,27
+27,18,62,53,55,11,59
+76,84,41,39,71,13,83,48,78,74,42
+42,27,77,88,38,18,17,37,97,46,53,45,11,57,31,68,61
+18,17,37,97,26,53,14,92,65,45,55,11,57,68,61,59,12,76,54,82,84
+88,26,45,55,57,31,76
+26,46,53,65,45,68,61,12,71,29,13
+61,18,82,11,57,31,17
+77,18,24,65,46,27,53,14,42
+84,41,39,71,73,56,23,51,78,74,96,42,27,77,88
+24,51,74,52,42,27,77,88,63,38,18,17,37,46,53,14,65
+24,23,51,74,96,52,42,77,88,63,38,18,62,17,37,97,26,46,53,14,92
+65,11,68,54,82
+48,74,96,42,88,38,62,14,92
+96,52,26,53,14,11,31
+97,26,53,14,92,65,55,11,31,61,59,76,54,84,39,71,29
+52,23,24,83,51,56,37,48,96,88,63
+46,11,57,39,54,68,31,37,55,82,26,59,76,12,65,53,92,45,41,97,14
+73,41,12,29,61,56,13,83,23,51,74,48,98,39,76,71,54,82,24
+56,48,24,78,96,27,77,88,38,18,62,17,37,97,46,53,14
+76,82,84,41,39,71,29,13,83,85,94,98,73,56,48,24,23,51,78,74,96,52,42
+42,18,94,27,13,29,56,96,24
+31,26,45,12,61,11,29,65,55,53,54,57,14,41,68,97,59,71,82,84,92,76,39
+45,82,12,76,84,53,18
+73,56,42,27,18,97,53
diff --git a/2024/05-Print_Queue/second.hs b/2024/05-Print_Queue/second.hs
new file mode 100644
index 0000000..1ff555f
--- /dev/null
+++ b/2024/05-Print_Queue/second.hs
@@ -0,0 +1,57 @@
+-- requires cabal install --lib megaparsec parser-combinators heap vector
+module Main (main) where
+
+import Control.Monad (void, when)
+import qualified Data.List as L
+import Data.Void (Void)
+import Text.Megaparsec
+import Text.Megaparsec.Char
+
+exampleExpectedOutput = 123
+
+type Rule = (Int, Int)
+type Update = [Int]
+data Input = Input [Rule] [Update] deriving Show
+
+type Parser = Parsec Void String
+
+parseNumber :: Parser Int
+parseNumber = read <$> some digitChar
+
+parseRule :: Parser Rule
+parseRule = (,) <$> parseNumber <* char '|'
+ <*> parseNumber <* eol
+
+parseUpdate :: Parser Update
+parseUpdate = some (parseNumber <* optional (char ',')) <* eol
+
+parseInput' :: Parser Input
+parseInput' = Input <$> some parseRule <* eol
+ <*> some parseUpdate <* eof
+
+parseInput :: String -> IO Input
+parseInput filename = do
+ input <- readFile filename
+ case runParser parseInput' filename input of
+ Left bundle -> error $ errorBundlePretty bundle
+ Right input' -> return input'
+
+compute :: Input -> Int
+compute (Input rules updates) = sum $ map compute' invalids
+ where
+ invalids = L.filter (not . valid) updates
+ valid update = all (valid' update) rules
+ valid' update (x, y) = case (L.elemIndices x update, L.elemIndices y update) of
+ ([i], [j]) -> i < j
+ _ -> True
+ compute' update = (L.sortBy sortByRule update) !! (length update `div` 2)
+ sortByRule :: Int -> Int -> Ordering
+ sortByRule x y = if L.elem (x, y) rules then LT else GT
+
+main :: IO ()
+main = do
+ example <- parseInput "example"
+ let exampleOutput = compute example
+ when (exampleOutput /= exampleExpectedOutput) (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
+ input <- parseInput "input"
+ print $ compute input