Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Project Euler problem 18/67 solver in Haskell

Name: Anonymous 2009-10-22 19:51


maximumPath :: [[Int]] -> Int
maximumPath tl = head $ foldl (flip reduce) (head rtl) (tail rtl)
  where rtl = reverse tl
        reduce [] b = []
        reduce a [] = []
        reduce (a:as) (b:c:bs) = (a + max b c) : reduce as (c:bs)
        reduce (a:as) [b] = [a + b]


To solve 67 (which is the more interesting one),


trif <- readFile "triangle.txt"
let answer = maximumPath $ map (\l -> map read l) (map words (lines trif))

Name: Anonymous 2009-10-27 1:22

W

Name: Anonymous 2009-10-27 1:22

P

Name: Anonymous 2009-10-27 1:22

O

Name: Anonymous 2009-10-27 1:23

K

Name: Anonymous 2009-10-27 1:23

E

Name: Anonymous 2009-10-27 1:51

M

Name: Anonymous 2009-10-27 1:51

O

Name: Anonymous 2009-10-27 1:51

N

Name: Anonymous 2009-10-27 1:52

Gotta catch 'em all!

Name: Anonymous 2009-10-28 11:35

>>1
maximumPath :: [[Int]] -> Int
maximumPath [] = error "Oh no, you called \"maximumPath []\"!"
maximumPath tl = head $ foldl reduce htl ttl
  where htl : ttl = reverse tl
        reduce [b] (a:_) = [a + b]
        reduce (b:c:cs) (a:as) = (a + max b c) : reduce (c:cs) as
        reduce _ _ = []

expand = map (map read . words) . lines

doToFile file = (readFile file >>=) . (print .)

main = doToFile "triangle.txt" $ maximumPath . expand

Name: Anonymous 2009-10-28 12:34

I got 99 problems but a one from project euler ain't one

Name: Anonymous 2009-10-28 13:03

>>51
a one

Name: Anonymous 2009-10-28 13:27

>>51
>>52
HIT ME

Name: Anonymous 2009-10-28 13:39

Name: Anonymous 2011-01-31 21:06

<-- check em dubz

Name: Anonymous 2011-02-03 2:57

Name: Anonymous 2011-02-04 15:54

Name: tray 2012-03-14 23:01


Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List