aboutsummaryrefslogtreecommitdiff
path: root/2020/02b.hs
diff options
context:
space:
mode:
authorAria <me@aria.rip>2023-01-02 21:58:56 +0000
committerAria <me@aria.rip>2023-01-02 21:58:56 +0000
commit5eb58ad076f2cd435b11b140820da224b60b73d5 (patch)
tree2a67939595fbf993ff04f69b9cd3f0aa20827d96 /2020/02b.hs
initial commit
Diffstat (limited to '2020/02b.hs')
-rw-r--r--2020/02b.hs31
1 files changed, 31 insertions, 0 deletions
diff --git a/2020/02b.hs b/2020/02b.hs
new file mode 100644
index 0000000..5754fe0
--- /dev/null
+++ b/2020/02b.hs
@@ -0,0 +1,31 @@
+module Day2 where
+
+import Data.List.NonEmpty (xor, fromList)
+import Data.List.Split (splitOn)
+import Text.Read (readMaybe)
+import System.Environment (getArgs)
+
+valid :: (Int, Int) -> Char -> String -> Bool
+valid (lo, hi) c s = xor $ fromList [s!!lo == c, s!!hi == c]
+
+parseHeader :: String -> (Int, Int, Char)
+parseHeader s = (lo, hi, c)
+ where [range, (c:_)] = splitOn " " s
+ [lo, hi] = map (\x -> x - 1) $ 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" 2b.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 ();