summaryrefslogtreecommitdiff
path: root/2019
diff options
context:
space:
mode:
Diffstat (limited to '2019')
-rw-r--r--2019/4.hs23
-rw-r--r--2019/4.in1
2 files changed, 24 insertions, 0 deletions
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]))
diff --git a/2019/4.in b/2019/4.in
new file mode 100644
index 0000000..693a14b
--- /dev/null
+++ b/2019/4.in
@@ -0,0 +1 @@
+123257-647015