-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.hs
31 lines (27 loc) · 1.15 KB
/
day1.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import Data.Text (splitOn, pack, unpack)
import Data.List (sortBy)
topThreeTotal :: String -> IO Int
topThreeTotal fileName = do topThree <- calCount fileName
let tot = foldl (+) 0 $ map (fst) topThree
return tot
calCount :: String -> IO [(Int, Int)]
calCount fileName = do contents <- readFile fileName
let lines = map (unpack) $ splitOn (pack "\n") (pack contents)
let elves = splitToElves lines
let elfTotals = totals elves
let elfTotals' = zip elfTotals [1..]
return (take 3 $ sortBy (\(x, _) (y, _) -> compare y x) elfTotals')
splitToElves :: [String] -> [[String]]
splitToElves s = case dropWhile p s of
[] -> []
s' -> w : splitToElves s''
where (w, s'') = break p s'
where p = \s -> s == ""
totals :: [[String]] -> [Int]
totals es =
let toInts :: [String] -> [Int]
toInts [] = []
toInts (s:ss) = read s : toInts ss
total :: [Int] -> Int
total is = foldl (+) 0 is
in map (total . toInts) es