Created
May 5, 2013 00:05
-
-
Save weskerfoot/5519210 to your computer and use it in GitHub Desktop.
Project Euler #11
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
import Data.List.Split | |
import Control.Applicative | |
import Control.Monad | |
rows :: String -> [[Double]] | |
rows xs = (map read) <$> splitOn " " <$> lines xs | |
columns :: [[Double]] -> [[Double]] | |
columns rs = map (map head) $ | |
takeWhile (any (/= [])) $ | |
iterate (map tail) rs | |
extendDiags' acc ds [] = (reverse acc) ++ ds | |
extendDiags' acc (d:ds) (x:xs) = extendDiags' ((x:d):acc) ds xs | |
extendDiags = extendDiags' [] | |
addDiags ds (c:col) = [c] : (extendDiags ds col) | |
diagonals' cs = foldl addDiags (map (:[]) (head cs)) (tail cs) | |
diagonals n k cs = | |
let ds = diagonals' cs | |
nd = n-(k*2)+1 | |
in take nd $ drop (k-1) ds | |
checkLength n xs = n == (length $ take n xs) | |
-- group the lists into groups of k size | |
groups k xs = | |
case checkLength k xs of | |
True -> | |
let g = take k xs in | |
g : (groups k $ tail xs) | |
_ -> [] | |
runsOf sideLength n matrix = | |
let rs = rows matrix | |
cs = columns rs | |
lds = diagonals sideLength n cs | |
rds = diagonals sideLength n (reverse cs) | |
in join $ groups 4 <$> rs ++ cs ++ lds ++ rds | |
main = do | |
matrix <- readFile "./grid" | |
print $ maximum $ product <$> runsOf 40 4 matrix |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment