Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2024-12-14 18:11:09 +05:30
parent 2cb456a7c4
commit 923e07eb44
3 changed files with 106 additions and 2 deletions

View File

@@ -1,13 +1,23 @@
module AoC where
import Data.List (tails, transpose)
import Text.Parsec (ParseError)
import Text.Parsec (ParseError, char, digit, many1, optionMaybe, (<|>))
import Text.Parsec.String (Parser)
-- extract Right value after parsing
extract :: Either ParseError a -> a
extract (Left err) = error ("Parsing failed: " ++ show err)
extract (Right val) = val
-- parse integer using parsec
parseSigned :: Parser Int
parseSigned = do
sign <- optionMaybe (char '-' <|> char '+')
num <- read <$> many1 digit
return $ case sign of
Just '-' -> negate num
_ -> num
-- count elements in a list
count :: (Eq a) => a -> [a] -> Int
count x = length . filter (x ==)