Skip to content

Instantly share code, notes, and snippets.

@ubergeek42
Created April 24, 2011 17:27
Show Gist options
  • Save ubergeek42/939724 to your computer and use it in GitHub Desktop.
Save ubergeek42/939724 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 (getScore 0 0 xs)
getScore :: Int->Int->[[Int]]->Int
getScore r c xs
| r >= (length xs) = 0
| c >= length (xs!!r) || c<0 = 0
| otherwise = ((xs!!r)!!c) + max left right
where
left = getScore (r+1) c xs
right = getScore (r+1) (c+1) xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment