summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2019-12-04 20:25:47 +0100
committertomsmeding <tom.smeding@gmail.com>2019-12-04 20:25:47 +0100
commit821c9a8c5209344538cadea879b3af6c8752e9bb (patch)
tree2b252db92c8b5dc247b55baa720c71d02f42968d
parent6e93d2e84649f233928ce011e48bef104114f243 (diff)
Day 4
-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