Search

I spent a lot of time thinking about problem 18.  I knew that a brute-force solution might work, but as the problem states, I would have to face a larger version later on, so it would be in my best interest to write a solution that could handle a larger set.  The trick to problem 18 (and 67) is to think from the bottom-up, instead of the top-down.  Given a three-number pyramid, it’s easy to see that the high sum path depends entirely on the larger of the two bottom numbers.  Once you know that, just work your way up the pyramid, two rows at a time.  The rest of the class is just parsing and setup code.

Problem 19 is stupidly easy using the DateTime class that .NET provides.  I mean, it was just so trivial it’s barely worth mentioning.

Problem 20 would take a lot more thinking and work, were it not for the BigInteger class in .NET 4.  Since I have that available, I just made a Factorial method and a method to parse out a string into digits and sum them up.

I was stuck on Problem 21 for a bit.  Note that I was getting to problem 21 soon after I finally got problem 11 solved.   The main block I had on problem 21 was that I was just overthinking it.  The answer was very simple once I just broke it up into a series of simple steps—a nice, elegant solution emerged.

I was at a “Code and Coffee” session when I solved problem 21.  I had time left, so I paired up to solved problem 22.  This solution was very concise to express with Linq.  The crux of the problem was figuring out a good way to get a score for each letter.  It turns out the syntax is very similar to C++ [c - (‘A’ – 1) was all it took to score a single letter].  Thanks to the power of Linq, a string can be viewed as a collection of characters, and thus the ‘Sum’ extension method works on it.

As always, you can view my code at CodePlex – feel free to submit criticisms/comments/patches there, or use the framework for your own Project Euler solution.

Leave a Reply