Tuesday, September 13, 2011

Project Euler 11 WIP

Project Euler Problem 11 is a pretty dramatic departure from the prior 10 problems (except maybe with the exception of problem 8).

I wrote out a smaller (6 x 6) version on paper and realized that it's not as hard as it initially looks. I'll have to brush up on my 2d array initialization.

The plan is at each element of the array to check if there are four numbers to the right, down, diagonal left, and diagonal right. That SHOULD handle every case of where there are four numbers lined up according to the rules of the problem. For example I don't need to check the diagonal to the upper left on element (6,5) because it was already handled by the diagonal lower right of element (3,2).

Traversing rows for checking or multiplying is easy. At element (3,2) the other three elements I need for the lower right diagonal are (4,3)(5,4)(6,5) - basically I just add +1 to the x and y three times. Similar rules apply for the other rows I need. I can likely combine the test of whether or not an element exists with the multiplying - if I try to add or subtract from an x or y point such that it falls out of bounds of the array I can simply not store the result and move on.

If I was feeling really fancy I'd do this in GTK and visualize the array and highlight the numbers I'm currently checking. It might be a good excuse to learn some Cairo.

No comments:

Post a Comment