Thursday, May 29, 2008

Google Treasure Hunt 2008 - Question 2

This is a continuation of my solutions to Google's treasure hunt. My post on question 1 can be found here.

The Question:
Here is a random zip archive for you to download:
GoogleTreasureHunt08_123456123456123456.zip

Unzip the archive, then process the resulting files to obtain a numeric result. You'll be taking the sum of lines from files matching a certain description, and multiplying those sums together to obtain a final result. Note that files have many different extensions, like '.pdf' and '.js', but all are plain text files containing a small number of lines of text.

Sum of line 2 for all files with path or name containing foo and ending in .xml
Sum of line 4 for all files with path or name containing mno and ending in .pdf
Hint: If the requested line does not exist, do not increment the sum.

Multiply all the above sums together and enter the product below.

Well this question begs for a [program,script,bash stmt] to be written. It doesn't even begin to make sense to do this manually..

Here's my Java source: http://b22222.com/files/GoogleQuestion2.java
One just needs to modify the set of rules and the location of the unzipped folder.

I just posted my answer for question 3; I have to wait 8 hours for the result! Oh well, fingers crossed.

Happy Googling!

Monday, May 19, 2008

Google Treasure Hunt 2008

So Google is running a treasure hunt. 1 puzzle a week for 4 weeks. I love puzzles.

The first puzzle they have up goes something like this:
A robot is located at the top-left corner of a 52 x 52 grid. The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid. How many possible unique paths are there?


For a while I tried to picture the mechanics of this problem in my head. It was too much,.. I would soon get lost trying to keep track of a dozen different paths and shapes all at once. So i resorted to paper. Not too long and I was having good progress. I would draw grids with numbers in each cell representing how many paths could be made from that cell to the bottom right. The trick was to start small.

I drew grids for 2x2 and 3x3. I still couldnt work out the pattern,.. Then I drew 2x3 and 3x2 grids; Bingo.



Overlay/Imageine a 3x2 and 2x3 grid at the bottom-right of the grid above; To work out the total number of paths from the last missing cell (of 3x3) is simply all paths available in both the 2x3 and 3x2 grids. Still dont understand,..
Simply put: The number of unique paths from any cell to the finish is equal to the sum of, the number of paths from the cell on the right, and the cell below it.

Here is the Java source I used to calculate my grid (55 x 60). My answer was 696940125414123253093858308567840.

I would be interested in looking over the mathematical formula that calculates this directly incase anyone happens to know it.

Happy treasure hunting!

PS: My code calculates the map upside down and other way around... In other words, the robot is going from bottom right to top left. Not the same as the picture above.

Tuesday, April 1, 2008

BR cleaner VS other cleaners

I've always got a bit of a mess on my hands when I'm finished CSS'ing an HTML form. With all the different forces shaping form development I sometimes find it difficult to get a form to look exactly the same in every browser, and to look pretty when degraded out of CSS.

Traditionally I've used divs or spans as a 'cleaner' element to clear the floats above it. I don't know where I learned this common trick, but for as long as I can remember now I've used the cleaner class and some empty divs round around the form to try correct line breaks and the form layout.

Completely randomely I tried something different today. I used a line break element as my cleaner. omg - Why have I never done this before? Now my line breaks act as line breaks when
CSS fails, and then they act as cleaners with CSS enabled. So far its been too good to be true,.. literally; I'm waiting to see why I haven't seen this elsewhere - Waiting to see where it breaks.

Here's a demo of what my cleaners now look like:

<style>
br {
display: none;
}
.cln {
display: block;
clear: both;
}
</style>
... blah blah fish paste ...
<div>My floated left element</div>
<div>My floated right element</div>
<br class="cln" />
<div>The rest of my content</div>



Some last notes:
I hide all line breaks by default because they are really there only for graceful degradation. If the client has CSS enabled then I don't want extra line breaks mucking up my presentation. On the other hand, when CSS is enabled then the linebreaks with the 'cln' class will be displayed and, simply put, line break over the above floated elements.

Wednesday, February 27, 2008

Time

For as long as I can remember now, time has only ever got faster. I never feel like I can do more than I used to - I'm always thinking how there just isn't as much time as there used to be.

Thursday, February 21, 2008

Powermate in C#

Well about four months ago I bought a Griffin Powermate. It's pretty much as good as they say except I think it could have been a little more solid. It looks a lot heavier in the pictures.

Now the only real gripe I had with the Powermate was the drivers. I battled for, I kid you not, days. I installed, connected, uninstalled, disconnected, reinstalled, reconnected this darn powermate so many times, that even by chance I should have got it to work.

The retailer I bought it from, SimplyMac.co.za in Johannesburg, wanted nothing to do with me. They simply denied me warranty on the item and told me to search Griffins site for help - Which I had already done by then! All I'm saying is that I will never buy from them again. Griffin was more helpfull,.. well nothing they suggested actually helped, but it didn't stop them from trying. I believe that if I carried on emailing their support team they probably would have got me to ship it over to get repaired.

I don't know how many people out there have battled with Powermates, but mine just seemed to move the cursor horizontally across the screen. The Powermate registers itself as a mouse-type device and thus without drivers will just cause the cursor to move slowly across the screen. With drivers and software you are meant to be able to assign more helpful actions to the Powermate; like changing the volume or scrolling your current document up and down. So since my Powermate made the mouse move without drivers, I reckon the problem must reside in the drivers. Surely if there was a fault in the Powermate itself then the device wouldn't register on the computer at all. Well whatever the problem is, shipping it over to the States is gonna cost be money. So some DIY was sounding good.

Yesterday I started snooping around C#, and hey,.. Presto! In half a day I managed to hack together a small app which:
1) Detects all the mouse HID's installed.
2) Links in to the data sent from the Powermate, and ..
3) Adjust the system volume or pump a page scroll.

Now I really did hack it together, I'm not even going to try take credit for the source. Having a good knowledge of Java and some C++ I managed to interpret the C# syntax without too much trouble but,... I'm not a C# guy so keep that in mind if you looking through the code.

Here's the source with binaries included: http://b22222.com/pmate.zip
Note: I coded this using MS Visual C# Express which I think uses dotNet 3.5

Directions for use:
Run PMate.exe (duh).
By default the Powermate then controls your volume.
If you press the Powermate it alternates to controlling page scroll.

The three thing's I'm not satisfied with yet in this solution are:
1) The program has a bit of delay while reading from the Powermate - Not ideal.
2) The Powermate still pumps regular mouse events. So pressing the Pmate still invokes context menus :/
3) I don't know how to properly hide the form, so it infact just minimised without a taskbar button. This allows you to still ALT-tab to the app.

At the end of the day though I'm quite happy with the solution. It gives me a bit of a geeky thrill turning the Powermate and knowing that I somehow got it to change my volume.