-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday1.hs
More file actions
30 lines (27 loc) · 766 Bytes
/
day1.hs
File metadata and controls
30 lines (27 loc) · 766 Bytes
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
import System.IO
import Control.Monad
import Data.List
main = do
let list = []
handle <- openFile "day1_input" ReadMode
contents <- hGetContents handle
let elves = lines contents
let list' = f elves [0]
let list = reverse list'
let max = maximum list
print "Max:"
print max
let sorted = sort list
let reversesorted = reverse sorted
let max3 = take 3 reversesorted
let max3sum = sum max3
print "Top 3 sum:"
print max3sum
hClose handle
readInt :: String -> Int
readInt [] = 0
readInt s = read s
f :: [String] -> [Int] -> [Int]
f [] is = is
f ([]:ss) is = f ss is ++ [0]
f (s:ss) is = init (f ss is) ++ [last (f ss is) + readInt s]