Try something like the following: -- Project Euler 11 -- In the 20×20 grid below, four numbers along a diagonal line have been marked in red. -- <snip> -- The product of these numbers is 26 × 63 × 78 × 14 = 1788696. -- What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 20×20 grid? import Data.List -- Doing the one dimensional case. f011 :: [Int] -> Int f011 (t:u:v:xs) = f011helper t u v xs f011helper :: Int -> Int -> Int -> [Int] -> Int f011helper t u v (w:ws) | ws == [] = t*u*v*w | otherwise = yada nada mada -- What are yada nada mada? -- The 20x20 grid case will become: f0112D :: [[Int]] -> Int -- where [[Int]] is a list of lists of rows, columns, major diagonals, & minor diagonals. On Sun, Aug 28, 2011 at 5:10 AM, Oscar Picasso <oscarpicasso@gmail.com> wrote:
No. The answer I posted is not good. It worked, by chance, on a couple of small examples I tried but it could end up comparing sequence of 4 numbers that where not initially adjacent.
On Sun, Aug 28, 2011 at 12:32 AM, Oscar Picasso <oscarpicasso@gmail.com> wrote:
Maybe this?
f x@(a:b:c:d:[]) = x f (a:b:c:d:e:ys) = if e >= a then f (b:c:d:e:ys) else f (a:b:c:d:ys)
On Sat, Aug 27, 2011 at 8:26 PM, KC <kc1956@gmail.com> wrote:
Think of the simplest version of the problem that isn't totally trivial.
e.g. A one dimensional list of numbers.
What would you do?
Note: you only want to touch each element once.
The 2 dimensional case could be handled by putting into lists: rows, columns, major diagonals, and minor diagonals.
This isn't the fastest way of doing the problem but it has the advantage of avoiding "indexitis".
-- -- Regards, KC