C solution. To compile and run:

$ gcc goal.c -o goal && ./goal

Reviewed by @tolmasky.
Merge pull request #3 from tolmasky/master

C solution