From 821c9a8c5209344538cadea879b3af6c8752e9bb Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Wed, 4 Dec 2019 20:25:47 +0100 Subject: Day 4 --- 2019/4.hs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 2019/4.hs (limited to '2019/4.hs') diff --git a/2019/4.hs b/2019/4.hs new file mode 100644 index 0000000..aaae87c --- /dev/null +++ b/2019/4.hs @@ -0,0 +1,23 @@ +module Main where + +import Input + + +main :: IO () +main = do + input <- head <$> getInput 4 + let (low', high') = fmap tail (span (/= '-') input) + (low, high) = (read low', read high') :: (Int, Int) + rdigits 0 = [] + rdigits n = n `mod` 10 : rdigits (n `div` 10) + rdigitpairs n = let l = rdigits n in zip l (tail l) + has2digit n = any (uncurry (==)) (rdigitpairs n) + nondecrease n = all (uncurry (>=)) (rdigitpairs n) + acceptable n = has2digit n && nondecrease n + print (length (filter acceptable [low..high])) + let has2digit' [] = False + has2digit' (a:b:c:xs) | a == b, b == c = has2digit' (dropWhile (== a) xs) + has2digit' (a:b:_) | a == b = True + has2digit' (_:xs) = has2digit' xs + acceptable' n = has2digit' (rdigits n) && nondecrease n + print (length (filter acceptable' [low..high])) -- cgit v1.2.3-70-g09d2