From 5eb58ad076f2cd435b11b140820da224b60b73d5 Mon Sep 17 00:00:00 2001 From: Aria Date: Mon, 2 Jan 2023 21:58:56 +0000 Subject: initial commit --- 2020/04a.hs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2020/04a.hs (limited to '2020/04a.hs') 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 -- cgit v1.2.3