Multiplying numbers on horizontal, vertial, and diagonal lines

Posted by untwisted on Stack Overflow See other posts from Stack Overflow or by untwisted
Published on 2010-05-07T23:25:41Z Indexed on 2010/05/07 23:58 UTC
Read the original article Hit count: 145

I'm currently working on a project Euler problem (www.projecteuler.net) for fun but have hit a stumbling block. One of the problem provides a 20x20 grid of numbers and asks for the greatest product of 4 numbers on a straight line. This line can be either horizontal, vertical, or diagonal.

Using a procedural language I'd have no problem solving this, but part of my motivation for doing these problems in the first place is to gain more experience and learn more Haskell.
As of right now I'm reading in the grid and converting it to a list of list of ints, eg -- [[Int]]. This makes the horizontal multiplication trivial, and by transposing this grid the vertical also becomes trivial.

The diagonal is what is giving me trouble. I've thought of a few ways where I could use explicit array slicing or indexing, to get a solution, but it seems overly complicated and hackey. I believe there is probably an elegant, functional solution here, and I'd love to hear what others can come up with.

© Stack Overflow or respective owner

Related posts about haskell

Related posts about functional-programming