From: Neil Smith Date: Wed, 19 Sep 2018 10:23:31 +0000 (+0100) Subject: Done task 2 X-Git-Url: https://git.njae.me.uk/?p=summerofcode2018soln.git;a=commitdiff_plain;h=9a82c8cdb3c4e92caa9d468045935e467c0117d8 Done task 2 --- diff --git a/data/02-rainfall.txt b/data/02-rainfall.txt new file mode 100644 index 0000000..c66e645 --- /dev/null +++ b/data/02-rainfall.txt @@ -0,0 +1,213 @@ +11 +12 +2 +4 +9 +8 +5 +0 +4 +9 +6 +0 +2 +8 +7 +7 +9 +7 +8 +10 +4 +7 +0 +4 +9 +2 +5 +0 +9 +6 +4 +11 +0 +10 +5 +0 +6 +4 +4 +11 +3 +8 +7 +8 +8 +9 +6 +12 +7 +2 +10 +5 +6 +5 +6 +2 +11 +6 +9 +10 +9 +9 +4 +10 +10 +7 +7 +8 +0 +10 +6 +0 +5 +4 +9 +7 +11 +3 +3 +2 +7 +5 +4 +5 +2 +8 +5 +7 +11 +9 +10 +7 +6 +7 +9 +7 +6 +0 +8 +5 +3 +0 +10 +1 +8 +8 +4 +3 +8 +2 +2 +7 +3 +8 +1 +5 +4 +10 +5 +9999 +14 +0 +4 +4 +12 +12 +17 +9 +19 +5 +23 +0 +1 +8 +18 +2 +4 +23 +9 +7 +7 +3 +16 +9 +14 +18 +20 +23 +16 +23 +19 +16 +15 +14 +22 +21 +10 +0 +20 +10 +11 +7 +3 +5 +20 +2 +4 +13 +3 +4 +5 +5 +3 +0 +12 +7 +20 +21 +3 +19 +24 +22 +21 +8 +5 +16 +23 +12 +14 +12 +4 +24 +6 +1 +14 +6 +2 +18 +9 +18 +5 +21 +12 +5 +5 +21 +23 +20 +20 +7 +12 +3 +3 diff --git a/src/task2/task2.hs b/src/task2/task2.hs new file mode 100644 index 0000000..3508d35 --- /dev/null +++ b/src/task2/task2.hs @@ -0,0 +1,26 @@ +import Data.List (foldl') + +main :: IO () +main = do + record_text <- readFile "data/02-rainfall.txt" + let records = readRecords record_text + print $ part1 records + print $ part2 records + print $ part2a records + +readRecords :: String -> [Int] +readRecords = takeWhile (< 9999) . map read . lines + +part1 :: [Int] -> Int +part1 = length + +part2 :: [Int] -> Int +part2 records = round $ ((fromIntegral $ sum records) :: Double) + / (fromIntegral $ length records) + + +-- Doing part 2 as a fold, so only traverse the list once +part2a :: [Int] -> Int +part2a records = round $ ((fromIntegral sumR) :: Double) / (fromIntegral count) + where + (sumR, count) = foldl' (\(s, n) r -> (s+r, n+1)) (0, 0) records diff --git a/summerofcode2018soln.cabal b/summerofcode2018soln.cabal index 715e6cb..c09637b 100644 --- a/summerofcode2018soln.cabal +++ b/summerofcode2018soln.cabal @@ -40,3 +40,9 @@ executable task1-mpc build-depends: base >= 4.7 && < 5 , text , megaparsec + +executable task2 + hs-source-dirs: src/task2 + main-is: task2.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5