From 6b507bc436a6d9134111f49abbd7b4a99aa114a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Depreeuw?= Date: Wed, 4 Dec 2019 10:11:44 +0100 Subject: [PATCH] Day 4 --- app/Main.hs | 5 +++++ src/Day4.hs | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/Day4.hs diff --git a/app/Main.hs b/app/Main.hs index fc76977..7d95a8f 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -3,6 +3,7 @@ module Main where import Day1 import Day2 import Day3 +import Day4 import System.IO import Data.List.Split import Data.Set @@ -41,4 +42,8 @@ main = do r_day3 <- readFile "./app/input_day3" putStr "[Day 3] Result: " print $ getDistanceAndSteps r_day3 + putStr "[Day 4-1] Result: " + print getNumberOfPasswords + putStr "[Day 4-2] Result: " + print getNumberOfPasswords' diff --git a/src/Day4.hs b/src/Day4.hs new file mode 100644 index 0000000..2311e8f --- /dev/null +++ b/src/Day4.hs @@ -0,0 +1,29 @@ +module Day4 + ( + getNumberOfPasswords + , getNumberOfPasswords' + ) where + +import Data.List + +generatePasswords_Part1 :: [Integer] +generatePasswords_Part1 = + [ x | x <- [145852..616942] + , has2Same (show x) + , increases ((read :: String -> Integer) . (:[]) <$> show x)] + where + has2Same xs = let ys = zip xs (tail xs ++ " ") in + any (uncurry (==)) ys + increases [] = True + increases xs = let ys = zip xs (tail xs ++ [maximum xs]) in + all (uncurry (<=)) ys + +getNumberOfPasswords :: Int +getNumberOfPasswords = length generatePasswords_Part1 + +generatePasswords_Part2 :: [Integer] +generatePasswords_Part2 = filter f generatePasswords_Part1 + where f x = 2 `elem` (length <$> group (show x)) + +getNumberOfPasswords' :: Int +getNumberOfPasswords' = length generatePasswords_Part2 \ No newline at end of file