Matt Hackmann

MattHackmann

The thoughts and goings-on of some programmer dad.

Writing Super Tiny Code

About a month or so ago, there was a contest thread on reddit whose objective was to write a PHP function to score a bowling game in as few characters as possible. A comma delimited string of frames would be passed in to the function and the score of said string would be returned.

My first step to this was to write, in normal code, a function that would do just that:

function dxprog($game) {
    $game = explode(',', $game);
    $len = count($game);
    $i = 0;
    $score = 0;
    $frame = 0;
    while ($frame < 10) {
        $frame++;
        $score += $game[$i];
        $next = $i + 1 < $len ? $game[$i + 1] : 0;
        $next2 = $i + 2 < $len ? $game[$i + 2] : 0;
        if ($game[$i] == 10) {
            $score += $next + $next2;
            $i++;
        } else if ($game[$i] < 10 && $game[$i] + $next == 10) {
            $score += $next;
            $score += $next2;
            $i += 2;
        } else {
            $score += $next;
            $i += 2;
        }
    }
    return $score;
}

496 characters. Obviously, the next step was to minify that code:

function dxprog($game) {
    $g=split(',',$game);$l=count($g);$i=$s=$f=0;while($f<10){$f++;$s+=$g[$i];$n=$i+1<$l?$g[$i+1]:0;$n2=$i+2<$l?$g[$i+2]:0;if($g[$i]==10){$s+=$n+$n2;$i++;}else if($g[$i]<10&&$g[$i]+$n==10){$s+=$n+$n2;$i+=2;}else{$s+=$n;$i+=2;}}return $s;
}

That brings us down to 232 characters. All of the minification was done by hand and now began the task of trimming as many characters as I could. Any reduction in characters, no matter how few, was welcomed. I reviewed my logic, rolled together declarations where I could and came up with the following 186 characters:

function dxprog($game) {
    $g=split(',',$game);$l=count($g);$i=$s=$f=0;while($f<10){$f++;$s+=$x=$g[$i];$o=$i+2<$l?$g[$i+2]:0;$n=++$i<$l?$g[$i]:0;if($x==10)$s+=$n+$o;else{$s+=$n;$i++;$s+=$x+$n==10?$o:0;}}return $s;
}

This challenge was pretty interesting because it required me to think in ways I never have before. In PHP, there's little reason to write small code, and in JavaScript, I'll just run it all through something like Closure Compiler, so this was something entirely new. What really helped reduce the code in the end was eliminating conditional branches that did the same thing (ternaries also helped immensely).

Lost Items Means Pictures of Cookies

Sadly, the mounting plate for my tripod seems to have gone missing which is really putting a damper on my video shooting ability (I fear I may have shipped it away with my camcorder).

So, in the stead of a video, here are photos of the peanut butter cookies I made after midnight last night. I had a hankering for cookies and, seeing as I have barred myself from stepping foot back in Wal-Mart again this week, conjured these up. I make the rules and if I decide I want cookies, I'll make some cookies regardless of the hour.

Lunch Musings - November 11, 2011

I was walking on my lunch break as I oft do and I saw sign with "11-1-11" scribbled on the back. This reminded me of the big stink that nerds raised last year on 11-11-11 and how it was the "last binary date of our generation".

But, nerds. You should know this wasn't true.

Yes, 111111 is 63 and, no, we're never going to encounter a date like that again in our lifetimes or in the next thousand years, for that matter. But for any nerd to try and assume that 11-11-11 is a valid date is stupid. There was this little thing a few years ago called Y2K.

You may recall it.

The whole issue with Y2K, for those who don't recall or never knew, was in the data depiction of years in a date. The first two digits were discarded and only the last two saved. Computers automatically stuffed a "19" in front and called it a day. Obviously, when those two stored digits were "00", all the dates would read 1900 and there would be mass pandemonium.

But, since the crossing of the (almost) new millennium, we've had to differentiate by storing the entire date, meaning all years would be stored/displayed as a four digit number.

So, in nerd terms, there never was a 11-11-11, just 11-11-2011 (or, 20111111 if you like being able to sort your dates numerically). And now that we can store dates ranging from 1/1/0001 to 12/31/9999, there was no excuse for the "whooaaahh" moment.

Now, January 19, 2038 03:14:08? That's something to look forward to.

When Your Foods Combine...

reddit is a great place to kill time with somewhat similarly like-minded people. It's a great place for comedy, feels, and elitism. As it turns out, it's also a great place for crazy awesome food ideas such as pizza cupcakes.

That photo series is scant on details, but there's really no recipe to follow here. I used the recipe on the back of a package of pizza yeast, stuffed into muffin tins, and filled with deliciousness - pizza sauce, Canadian bacon, Italian sausage, and mozzarella (the stuff you find in the hispanic section, not shredded). Stuff in a 400F oven for 10-15 minutes (depending on how you like your crust) and you get amazingness:

I Has a Shiny New Toy

As it turns out, FedEx delivers Tuesday through Saturday, so my lost weekend is less so.

I've been wanting a decent camera for some time as the point and shoots do not deliver the kind of quality I would prefer (and tend to fall apart rather quickly). I was actually planning on picking up a dSLR last year before the anime convention, but, I couldn't find one for a decent enough price that also did video. The answer to that problem, apparently, is to simply wait a year.

Fast forward to now - still wanting a good camera for the con - and the camera I've purchased, a Canon Rebel T3i retails for $750, well under the lower end numbers from last year. Toss in some clever shopping about and selling off of soon-to-be deprecated hardware (my old camcorder), and that $750 is brought down to a much more tolerable $350.

Even at $750, it would have been worth every goddamn penny. The picture above was taken with the flash off in my poorly lit bathroom. AND IT STILL LOOKS AMAZING (my troll face notwithstanding)!

I've not yet played around with the video option much, but expect some new movies this month now with fancy, movie style rack focus!