haskell pinball
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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