Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Project Euler - Problem 11 - Greatest product in grid
import Data.List(transpose,foldl')
subGridSize = 4
main = print
=<< return . result
=<< readFile "pb011-grid.txt"
result cs = maximum $ map maxGrid $ subgrids subGridSize
$ map (map read . words) $ lines cs
subgrids n = map transpose . concat .
map (slice n . transpose) . slice n
where slice n [] = []
slice n xs = takeWhile ((== n) . length)
$ (take n xs) : (slice n $ tail xs )
maxGrid xs = maximum [maxRows xs,
maxCols xs,
productMainDiagonal xs,
productSecondDiagonal xs]
where maxRows = maximum . map product
maxCols = maxRows . transpose
productMainDiagonal xs =
product [(xs!!i)!!i|i<-[0..(length xs)-1]]
productSecondDiagonal xs =
product [(xs!!i)!!(lasti-i)|i<-[0..lasti]]
lasti = (length xs) - 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.