Done task 2
authorNeil Smith <neil.git@njae.me.uk>
Wed, 19 Sep 2018 10:23:31 +0000 (11:23 +0100)
committerNeil Smith <neil.git@njae.me.uk>
Wed, 19 Sep 2018 10:23:31 +0000 (11:23 +0100)
data/02-rainfall.txt [new file with mode: 0644]
src/task2/task2.hs [new file with mode: 0644]
summerofcode2018soln.cabal

diff --git a/data/02-rainfall.txt b/data/02-rainfall.txt
new file mode 100644 (file)
index 0000000..c66e645
--- /dev/null
@@ -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 (file)
index 0000000..3508d35
--- /dev/null
@@ -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
index 715e6cbdc43385bff8ead08af0d9482a061ad910..c09637bdfd041f0e10518a190bd76011b2c369e7 100644 (file)
@@ -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