Skip to content

Instantly share code, notes, and snippets.

@rkrzewski
Created January 21, 2015 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkrzewski/f0cf3b6f12d11563c82e to your computer and use it in GitHub Desktop.
Save rkrzewski/f0cf3b6f12d11563c82e to your computer and use it in GitHub Desktop.
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