2020-02 in haskell

This commit is contained in:
Julien Dessaux 2023-03-08 23:21:52 +01:00
parent 96efc40e53
commit 662b77da67
Signed by: adyxax
GPG key ID: F92E51B86E07177E
4 changed files with 1122 additions and 0 deletions

View file

@ -0,0 +1,3 @@
1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc

View file

@ -0,0 +1,59 @@
-- requires cabal install --lib megaparsec
module Main (main) where
import Control.Monad (void, when)
import Data.Void
import Text.Megaparsec
import Text.Megaparsec.Char
import System.Exit (die)
exampleExpectedOutput = 2
data Rule = Rule { lower :: Int
, higher :: Int
, elt :: Char
, pass :: String
} deriving (Show)
type Parser = Parsec Void String
parseRule :: Parser Rule
parseRule = do
l <- try (some digitChar)
void (char '-')
h <- try (some digitChar)
void (char ' ')
e <- anySingle
void (string ": ")
pass <- (many letterChar)
void (char '\n')
return Rule { lower = read l, higher = read h, elt = e, pass = pass }
parseRules :: Parser [Rule]
parseRules = do
rules <- many parseRule
eof
return rules
parseInput :: String -> IO [Rule]
parseInput filename = do
input <- readFile filename
case runParser parseRules filename input of
Left bundle -> die $ errorBundlePretty bundle
Right rules -> return rules
validateRule :: Rule -> Int
validateRule Rule{lower=l, higher=h, elt=e, pass=pass} = if (n >= l && n <= h) then 1 else 0
where
n = length $ filter (== e) pass
compute :: [Rule] -> Int
compute = sum . map validateRule
main :: IO ()
main = do
example <- parseInput "example"
let exampleOutput = compute example
when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
input <- parseInput "input"
print $ compute input

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,60 @@
-- requires cabal install --lib megaparsec
module Main (main) where
import Control.Monad (void, when)
import Data.Void
import Text.Megaparsec
import Text.Megaparsec.Char
import System.Exit (die)
exampleExpectedOutput = 1
data Rule = Rule { lower :: Int
, higher :: Int
, elt :: Char
, pass :: String
} deriving (Show)
type Parser = Parsec Void String
parseRule :: Parser Rule
parseRule = do
l <- try (some digitChar)
void (char '-')
h <- try (some digitChar)
void (char ' ')
e <- anySingle
void (string ": ")
pass <- (many letterChar)
void (char '\n')
return Rule { lower = (read l) - 1, higher = (read h) - 1, elt = e, pass = pass }
parseRules :: Parser [Rule]
parseRules = do
rules <- many parseRule
eof
return rules
parseInput :: String -> IO [Rule]
parseInput filename = do
input <- readFile filename
case runParser parseRules filename input of
Left bundle -> die $ errorBundlePretty bundle
Right rules -> return rules
validateRule :: Rule -> Int
validateRule Rule{lower=l, higher=h, elt=e, pass=pass} = if (a /= b) then 1 else 0
where
a = e == pass !! l
b = e == pass !! h
compute :: [Rule] -> Int
compute = sum . map validateRule
main :: IO ()
main = do
example <- parseInput "example"
let exampleOutput = compute example
when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
input <- parseInput "input"
print $ compute input