aboutsummaryrefslogtreecommitdiff
path: root/2020/04a.hs
diff options
context:
space:
mode:
Diffstat (limited to '2020/04a.hs')
-rw-r--r--2020/04a.hs30
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