diff options
author | Aria <me@aria.rip> | 2023-01-02 21:58:56 +0000 |
---|---|---|
committer | Aria <me@aria.rip> | 2023-01-02 21:58:56 +0000 |
commit | 5eb58ad076f2cd435b11b140820da224b60b73d5 (patch) | |
tree | 2a67939595fbf993ff04f69b9cd3f0aa20827d96 /2020/04a.hs |
initial commit
Diffstat (limited to '2020/04a.hs')
-rw-r--r-- | 2020/04a.hs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/2020/04a.hs b/2020/04a.hs new file mode 100644 index 0000000..09dd5c3 --- /dev/null +++ b/2020/04a.hs @@ -0,0 +1,30 @@ +module Day4a where + +import System.Environment (getArgs) +import Data.List.Split (splitOn) + +-- Split a passport declaration into k:v declarations +getDeclarations :: String -> [String] +getDeclarations xs = concat [splitOn "\n" y | y <- splitOn " " xs] + +-- Split a passport declaration into tuples of (key, value) +getTuples :: String -> [(String, String)] +getTuples xs = [(head x, x!!1) | x <- map (splitOn ":") $ getDeclarations xs] + +-- Check a passport is valid +passportValid :: String -> Bool +passportValid xs = and [r `elem` map fst tups | r <- reqs] + where tups = getTuples xs + reqs = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"] + +-- Usage: runghc --ghc-arg="-package split" 4a.hs inputs/day4 +main :: IO () +main = do + args <- getArgs; + content <- readFile $ head args; + + let ps = splitOn "\n\n" content; + let valid = filter passportValid ps; + + print $ length valid; + return ();
\ No newline at end of file |