Skip to content

Instantly share code, notes, and snippets.

@fbiville
Created January 5, 2015 10:32
Show Gist options
  • Select an option

  • Save fbiville/8c13854f581b9fd35e2e to your computer and use it in GitHub Desktop.

Select an option

Save fbiville/8c13854f581b9fd35e2e to your computer and use it in GitHub Desktop.
Warning: dumb (& dumber)
greatestProduct :: [[Int]] -> Int
greatestProduct mat = _greatestProduct (0,0) mat 0
_greatestProduct :: (Int,Int) -> [[Int]] -> Int -> Int
_greatestProduct (20,y) mat prev = prev
_greatestProduct coord mat prev
| current > prev = _greatestProduct nextC mat current
| otherwise = _greatestProduct nextC mat prev
where nextC = (nextCoord coord)
maxVert = (max (up coord mat) (down coord mat))
maxHor = (max (left coord mat) (right coord mat))
max1 = (max maxVert maxHor)
maxDiagUp = (max (upLeft coord mat) (upRight coord mat))
maxDiagDown = (max (downLeft coord mat) (downRight coord mat))
max2 = (max maxDiagUp maxDiagDown)
current = (max max1 max2)
up :: (Int,Int) -> [[Int]] -> Int
up (x,y) mat
| x < 3 = 0
| otherwise = (mat !! x !! y) * (mat !! (x-1) !! y) * (mat !! (x-2) !! y) * (mat !! (x-3) !! y)
down :: (Int,Int) -> [[Int]] -> Int
down (x,y) mat
| x > 16 = 0
| otherwise = (mat !! x !! y) * (mat !! (x+1) !! y) * (mat !! (x+2) !! y) * (mat !! (x+3) !! y)
right :: (Int,Int) -> [[Int]] -> Int
right (x,y) mat
| (y > 16) = 0
| otherwise = (x0 !! y) * (x0 !! (y+1)) * (x0 !! (y+2)) * (x0 !! (y+3))
where x0 = head mat
left :: (Int,Int) -> [[Int]] -> Int
left (x,y) mat
| (y < 3) = 0
| otherwise = (x0 !! y) * (x0 !! (y-1)) * (x0 !! (y-2)) * (x0 !! (y-3))
where x0 = head mat
upLeft :: (Int,Int) -> [[Int]] -> Int
upLeft (x,y) mat
| (x < 3) || (y < 3) = 0
| otherwise = (mat !! x !! y) * (mat !! (x-1) !! (y-1)) * (mat !! (x-2) !! (y-2)) * (mat !! (x-3) !! (y-3))
upRight :: (Int,Int) -> [[Int]] -> Int
upRight (x,y) mat
| (x < 3) || (y > 16) = 0
| otherwise = (mat !! x !! y) * (mat !! (x-1) !! (y+1)) * (mat !! (x-2) !! (y+2)) * (mat !! (x-3) !! (y+3))
downLeft :: (Int,Int) -> [[Int]] -> Int
downLeft (x,y) mat
| (x > 16) || (y < 3) = 0
| otherwise = (mat !! x !! y) * (mat !! (x+1) !! (y-1)) * (mat !! (x+2) !! (y-2)) * (mat !! (x+3) !! (y-3))
downRight :: (Int,Int) -> [[Int]] -> Int
downRight (x,y) mat
| (x > 16) || (y > 16) = 0
| otherwise = (mat !! x !! y) * (mat !! (x+1) !! (y+1)) * (mat !! (x+2) !! (y+2)) * (mat !! (x+3) !! (y+3))
nextCoord :: (Int,Int) -> (Int,Int)
nextCoord coord = let y' = (snd coord) + 1
x = (fst coord)
newx = (if (y' < 20) then x else (x+1))
newy = (y' `mod` 20) in (newx,newy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment