Matt Hackmann

MattHackmann

The thoughts and goings-on of some programmer dad.

Solving Inane Issues with Programming

I'm slowly running out of things to say for the remainder of the month and the most interesting stuff is still five days out. Also, my brother and I have run out of things to watch, having finished seven seasons of How I Met Your Mother and rewatched both seasons of Higurashi in about a couple months. We're about to start watching Clannad again, but there's actually a simple solution to both of these problems.

And that solution is programming.

I may have mentioned before, but about half a mile up from work is a branch of the local library. They have a pretty decent collection of Blu-rays. So, I wrote a small script to scrape all the blu-ray titles at that location and, at random, pick four movies for us to watch. Here's what it came up with:

  1. Seven Years in Tibet
  2. Star Trek - The Motion Picture
  3. The Silence of the Lambs
  4. 9

Number 4 was originally Superman Returns, but my brother and I have already seen that and so decided to reroll.

I suppose out of a data set of only 460+ there was bound to be one movie I'd recognize, but not all four.

So, each day on my lunch break, I will bravely trek up to the library to fetch one of these discs. If one shouldn't be available, I'll move to the next item on the list. If I go past the end, a new movie will be randomly selected. This movie will then be watched and I will give a short review of my feelings at the end.

Seriously, being a programmer is akin to being a wizard in the Harry Potter world. I don't know how you muggles make it...

Eventualities

A while back, shortly before my leaving Griffin, my French coworker brought a bunch of stuff that was eating up space in his house and pawned it off to the rest of us. There was no exchange of money, but many lunch battles were had.

One of the items was a Japanese microwave potato chip maker. He'd never used it himself, so passed it on to me to be the brave one to try such odd novelties. I accepted his challenge to give it a whirl.

And then it sat in my closet for over a year.

That is, until today when, in a fit of super boredom, I decided that giving this thing a shot would be a good way to kill some minutes and also satiate my rumbling stomach (didn't work, wound up at Taco Bell anyways).

Continue Reading

After Last Night's Escapades...

I got shit faced after making last night's comic. Then I threw up. Immediately afterwards,I wrote this on the board.

It's okay, though. My brother already had a bowl standing by and my aim was true.

The Whiteboard

Above my second monitor and slightly to the right is my brain's physical scratch disk, aka my whiteboard. It's there for ideas I don't want to forget or for figuring stuff out.

Continue Reading

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).