From 8274d687be2b87501b9f7828b21f12732d640c46 Mon Sep 17 00:00:00 2001 From: sgf Date: Sat, 11 Dec 2021 00:08:05 +0300 Subject: [PATCH] new(haskell): Solved day 1 second puzzle. --- day1/Main.hs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/day1/Main.hs b/day1/Main.hs index d571ad2..d3eac3d 100644 --- a/day1/Main.hs +++ b/day1/Main.hs @@ -1,10 +1,25 @@ - f1 :: [Int] -> Int f1 [] = 0 +-- y x(cur) f1 (k : ks) = foldr (\x g y -> let n = g x in if y < x then n + 1 else n) (const 0) ks k +f2 :: [Int] -> Int +f2 [] = 0 +f2 (k1 : k2 : ks) = fst $ foldr go (\_ _ -> (0, 0)) ks k1 k2 + where + go :: + Int -> + (Int -> Int -> (Int, Int)) -> + (Int -> Int -> (Int, Int)) + -- w y x(cur) + go x g w y = + let (n, next) = g y x + cur = w + y + x + in if next > cur then (n + 1, cur) else (n, cur) + main :: IO () main = do - c <- readFile "day1/input.txt" - let xs = map read (lines c) - print (f1 xs) + c <- readFile "day1/input.txt" + let xs = map read (lines c) + print $ "Answer1: " ++ show (f1 xs) + print $ "Answer2: " ++ show (f2 xs) -- 2.20.1