From 5eb58ad076f2cd435b11b140820da224b60b73d5 Mon Sep 17 00:00:00 2001 From: Aria Date: Mon, 2 Jan 2023 21:58:56 +0000 Subject: initial commit --- 2020/02a.hs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 2020/02a.hs (limited to '2020/02a.hs') diff --git a/2020/02a.hs b/2020/02a.hs new file mode 100644 index 0000000..3671403 --- /dev/null +++ b/2020/02a.hs @@ -0,0 +1,31 @@ +module Day2 where + +import Data.List.Split (splitOn) +import Text.Read (readMaybe) +import System.Environment (getArgs) + +valid :: (Int, Int) -> Char -> String -> Bool +valid (lo, hi) c s = len >= lo && len <= hi + where len = length (filter (== c) s) + +parseHeader :: String -> (Int, Int, Char) +parseHeader s = (lo, hi, c) + where [range, (c:_)] = splitOn " " s + [lo, hi] = map read $ splitOn "-" range + +lineValid :: String -> Bool +lineValid s = valid (lo, hi) c pass + where [header, pass] = splitOn ": " s + (lo, hi, c) = parseHeader header + +-- Usage: runghc --ghc-arg="-package split" 2a.hs inputs/day2 +main :: IO () +main = do + args <- getArgs; + content <- readFile $ head args; + let l = lines content; + let nums = filter lineValid l + + putStrLn $ show $ length nums; + + return (); \ No newline at end of file -- cgit v1.2.3