Skip to content

Instantly share code, notes, and snippets.

@Osmose
Forked from ubergeek42/gist:939724
Created April 24, 2011 17:44
Show Gist options
  • Save Osmose/939736 to your computer and use it in GitHub Desktop.
Save Osmose/939736 to your computer and use it in GitHub Desktop.
haskell pinball
{-# OPTIONS_GHC -Wall -O2 #-}
module Main where
main :: IO()
main = interact (solve . readTestCases)
-- This builds a list of lists representing the problem
readTestCases :: String -> [[Int]]
readTestCases = parse 1 . map read . words
-- Input: Row#, list of ints from input
-- Output: returns a list of lists
parse :: Int->[Int]->[[Int]]
parse _ [] = []
parse 1 (_:x:xs) = [x]:parse 2 xs
parse i xs = (take i xs):parse (i+1) (drop i xs)
-- Solve the problem for one test case
solve :: [[Int]] -> String
solve xs = show (head $ getScoreForRow xs 0)
getScoreForRow:: [[Int]] -> Int -> [Int]
getScoreForRow all num
| num == ((length all) - 1) = all !! num
| otherwise = cur
where
prev = getScoreForRow all (num + 1)
cur = map (getScore prev (all !! num)) [0..(num)]
getScore :: [Int]->[Int]->Int->Int
getScore prev cur c = (cur!!c) + max left right
where
left = prev !! c
right = prev !! (c+1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment