Code Golf 2

2 minute read

At the York Code Dojo this week, the challenge was to create a small program that output the childrens nursery rhyme “There was an old lady who swallowed a fly…”

Myself and my partner Mahy approached it using C++.

Our solution works by making an array of commonly used words and splicing them together using some rather convoluted code! I think it could still go a little smaller, but it does get hard to tweak the more ‘optimal’ you make it. There are some repeated words that could probably save a bit more. The final size of the program is somewhere below 700 bytes.

A few simple tricks:

  • The macro saves characters, and completes the previous line
  • There is in inner loop for the repeated verse which selects pairs of animals
  • initializing the ints globaly sets them to zero (something I’d forgotten)

The point of this exercise is to get you to think outside the box, and gain a deeper understanding of the limitations & features of the language you choose. There’s a theory that says our perception of the world is based on the internal language we use to describe it. Certainly, it is true for programming languages; they each have their quirks.

#include<iostream>
#define Q ;std::cout<<u
char*u[]{"There was an old lady ","who ","that ","swallowed a ","She swallowed the ","\nI don't know why she ","erhaps she'll die!\n","",",\nThat wriggled and wiggled and tiggled inside her;\n",";\nHow absurd",";\nFancy that",";\nWhat a hog,",",\nI don't know how she swallowed a "," to catch the ","fly","spider","bird","cat","dog","cow","horse...\nShe's dead, of course!","\n",",",";"," - p"," - P"," to swallow a ",".!;;"};
int i,j;int main()
{
    for(;i<7;i++)
    {
        Q[0]Q[i==4?2:1]Q[3]Q[14+i];
        if(i<6)
        {
            if(i<2)
            {
                Q[7+i];
            }
            else
            {
                Q[7+i]Q[i<5?26:7]Q[i+14]Q[27][i-2]Q[21];
            }
            for(j=i;j;j--)
            {
                Q[4]Q[14+j]Q[13]Q[13+j]Q[j==1?23:22]Q[j==1?7:21];
            }
            Q[5]Q[3]Q[14]Q[!i?24:25]Q[6];
        }
    }
}
There was an old lady who swallowed a fly
I don't know why she swallowed a fly - Perhaps she'll die!
There was an old lady who swallowed a spider,
That wriggled and wiggled and tiggled inside her;
She swallowed the spider to catch the fly;
I don't know why she swallowed a fly - Perhaps she'll die!
There was an old lady who swallowed a bird; 
How absurd to swallow a bird.
She swallowed the bird to catch the spider,
She swallowed the spider to catch the fly;
I don't know why she swallowed a fly - Perhaps she'll die!
There was an old lady who swallowed a cat; 
Fancy that to swallow a cat!
She swallowed the cat to catch the bird,
She swallowed the bird to catch the spider,
She swallowed the spider to catch the fly;
I don't know why she swallowed a fly - Perhaps she'll die!
There was an old lady that swallowed a dog; 
What a hog, to swallow a dog;
She swallowed the dog to catch the cat,
She swallowed the cat to catch the bird,
She swallowed the bird to catch the spider,
She swallowed the spider to catch the fly;
I don't know why she swallowed a fly - Perhaps she'll die!
There was an old lady who swallowed a cow,
I don't know how she swallowed a cow;
She swallowed the cow to catch the dog,
She swallowed the dog to catch the cat,
She swallowed the cat to catch the bird,
She swallowed the bird to catch the spider,
She swallowed the spider to catch the fly;
I don't know why she swallowed a fly - Perhaps she'll die!
There was an old lady who swallowed a horse...
She's dead, of course!

Updated:

Leave a Comment