From: sgf Date: Fri, 10 Dec 2021 21:08:05 +0000 (+0300) Subject: new(haskell): Solved day 1 second puzzle. X-Git-Url: https://gitweb.sgf-dma.tk/?a=commitdiff_plain;h=8274d687be2b87501b9f7828b21f12732d640c46;p=aoc-2021.git new(haskell): Solved day 1 second puzzle. --- 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)