-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp067.hs
29 lines (24 loc) · 930 Bytes
/
p067.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
import Data.List
import Data.Char
import System.IO
tuplePair :: [a] -> [(a,a)]
tuplePair (x:[]) = [(x,x)]
tuplePair (x:y:xs) = (x,y) : tuplePair (y:xs)
pickOption :: Int -> (Int, Int) -> Int
pickOption x options = maximum [first, second]
where first = x + (fst options)
second = x + (snd options)
process :: [[Int]] -> [Int]
process (x:[]) = x
process (x:y:xs) = process (calcRow : xs)
where expandedRow = tuplePair $ (head x) : x
calcRow = zipWith pickOption y expandedRow
readTriangleLine :: String -> [Int]
readTriangleLine [] = []
readTriangleLine (xs) = digit : readTriangleLine (drop 2 dropSpaces)
where dropSpaces = dropWhile (==' ') xs
digit = read $ takeWhile (\x -> elem x ['0'..'9']) dropSpaces
main = do
handle <- openFile "p067_triangle.txt" ReadMode
contents <- hGetContents handle
print $ maximum $ process $ map readTriangleLine $ lines contents