Created
January 21, 2015 19:00
-
-
Save rkrzewski/f0cf3b6f12d11563c82e to your computer and use it in GitHub Desktop.
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
object Euler11 { | |
def collect(grid: Array[Array[Int]], x: Int, y: Int, dx: Int, dy: Int, l: Int, acc: Seq[Int]): Seq[Int] = | |
if (l <= 0 || x < 0 || y < 0 || y >= grid.length || x >= grid(y).length) acc | |
else collect(grid, x + dx, y + dy, dx, dy, l - 1, grid(y)(x) +: acc) | |
def chains(grid: Array[Array[Int]], l: Int): Seq[Seq[Int]] = | |
for { | |
(dx, dy) <- Seq((1, 0), (0, 1), (1, 1), (-1, 1)) | |
y0 <- 0 until grid.length | |
x0 <- 0 until grid(y0).length | |
chain = collect(grid, x0, y0, dx, dy, l, Seq()) | |
if chain.length == l | |
} yield chain | |
def largestProductInGrid(grid: Array[Array[Int]], length: Int): Int = | |
chains(grid, length).map(_.product).sorted.last | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment