From 36ccd6f974b0f3382856d5c13ee5a97ddc14cba8 Mon Sep 17 00:00:00 2001 From: shu Date: Tue, 1 Dec 2020 09:13:22 +0100 Subject: [PATCH] day01: prettier solution --- 2020/day01/day01.hs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/2020/day01/day01.hs b/2020/day01/day01.hs index b4bcd41..c98a12c 100644 --- a/2020/day01/day01.hs +++ b/2020/day01/day01.hs @@ -1,11 +1,13 @@ +import Control.Arrow +import Control.Monad + main :: IO () main = do - input <- map read . lines <$> readFile "input" - print (day1a input) - print (day1b input) + input <- map read . lines <$> readFile "input" + print $ day1 input 2 + print $ day1 input 3 -day1a :: [Int] -> Int -day1a input = head [x * y | x<-input, y<-input, x+y==2020] - -day1b :: [Int] -> Int -day1b input = head [x * y * z | x<-input, y<-input, z<-input, x+y+z==2020] +day1 :: [Int] -> Int -> Int +day1 input n = + snd . head . filter ((== 2020) . fst) $ + map (sum &&& product) $ replicateM n input