2024-05 in haskell
This commit is contained in:
parent
4dee621ccf
commit
3d7a069b71
4 changed files with 1526 additions and 0 deletions
28
2024/05-Print_Queue/example
Normal file
28
2024/05-Print_Queue/example
Normal file
|
@ -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
|
55
2024/05-Print_Queue/first.hs
Normal file
55
2024/05-Print_Queue/first.hs
Normal file
|
@ -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
|
1386
2024/05-Print_Queue/input
Normal file
1386
2024/05-Print_Queue/input
Normal file
File diff suppressed because it is too large
Load diff
57
2024/05-Print_Queue/second.hs
Normal file
57
2024/05-Print_Queue/second.hs
Normal file
|
@ -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
|
Loading…
Add table
Reference in a new issue