Jonathan Boccara's blog

The Most Beautiful Piece of Code That Prints 42

Published May 11, 2018 - 18 Comments

Two weeks from now, on the 25th of May, is a very important event in the geek culture: Towel Day, and I’d love to celebrate it with you on Fluent C++!

EDIT: check out the most beautiful piece of code that print 42 here!

towel day C++

Towel day?

Towel day is a yearly tribute to the fiction work of Douglas Adams called Hitchhiker’s Guide to the Galaxy. This is a series of novels (a “trilogy in 5 parts” to be precise) that follows the adventures of Arthur Dent, a human that was saved just before the Earth was destroyed by the Vogons to make way for an intergalactic bypass.

The Hitchhiker’s Guide to the Galaxy

In the story, the destruction of the Earth is very annoying for mice, which happen to be hyper-intelligent pan-dimensional beings (they managed to make humans believe that they were experimenting on mice, while it was  in fact the other way around), because they are at the origin of the Earth’s creation.

It all started with an extremely powerful computer called Deep Thought that the mice built to work out the Answer to the Ultimate Question of Life, the Universe, and Everything. And the answer turns out to be 42. However, Deep Thought cannot tell what the Ultimate Question was.

So the mice go and build an even more powerful computer, often mistaken for a planet because of its shape and biology. This is the Earth. Unfortunately as told above, the Earth is destroyed by Vogons for the highway bypass before it can finish its computation.

A piece of geek culture

The Hitchhiker’s Guide to the Galaxy is hugely popular in the geek culture (and also beyond it). For example, when we need a number to illustrate something in code, we often pick 42. This is where this habit comes from.

But the book is not about software development, so why do software developers identify themselves with it so much?

It’s hard to tell, but the thing is if you’re a geek, or if you’re identifying yourself with the geek culture, there is a high chance that you’ll like it. Maybe it’s because the imagination and creativity of the author will seduce you. Or because of the controlled absurdity of the story. Or perhaps it’s the geek humour, if there’s such a thing (when I read the book, I laugh out loud every couple of minutes!)

(If you’re interested in checking out the books, I refer to them at the end of this article)

Towel day

If you’re hitchhiking the galaxy, the most important piece of your equipment happens to be your towel.

Here is a famous quote from the Hitchhiker’s Guide to the Galaxy that says why:

towel dayA towel, it says, is about the most massively useful thing an interstellar hitchhiker can have. Partly it has great practical value. You can wrap it around you for warmth as you bound across the cold moons of Jaglan Beta; you can lie on it on the brilliant marble-sanded beaches of Santraginus V, inhaling the heady sea vapors; you can sleep under it beneath the stars which shine so redly on the desert world of Kakrafoon; use it to sail a miniraft down the slow heavy River Moth; wet it for use in hand-to-hand-combat; wrap it round your head to ward off noxious fumes or avoid the gaze of the Ravenous Bugblatter Beast of Traal (such a mind-bogglingly stupid animal, it assumes that if you can’t see it, it can’t see you); you can wave your towel in emergencies as a distress signal, and of course dry yourself off with it if it still seems to be clean enough.

More importantly, a towel has immense psychological value. For some reason, if a strag (strag: non-hitch hiker) discovers that a hitch hiker has his towel with him, he will automatically assume that he is also in possession of a toothbrush, face flannel, soap, tin of biscuits, flask, compass, map, ball of string, gnat spray, wet weather gear, space suit etc., etc. Furthermore, the strag will then happily lend the hitch hiker any of these or a dozen other items that the hitch hiker might accidentally have “lost”. What the strag will think is that any man who can hitch the length and breadth of the galaxy, rough it, slum it, struggle against terrible odds, win through, and still knows where his towel is, is clearly a man to be reckoned with.” – The Hitchhiker’s Guide to the Galaxy

Since the death of Doug Adams in 2001, a lot of people across the planet celebrate “Towel Day” every year on the 25th of May. It’s a big thing. People go around wearing their towel on that day. There’s even an official Hitchhiker’s Guide to the Galaxy Appreciation Society in Islington that meets up on that day to celebrate.

Now that you’re up-to-date on what Towel Day is, let’s celebrate on Fluent C++!

The most beautiful piece of code that prints 42

42 towel day C++The Hitchhiker’s Guide is about 42, Fluent C++ is about expressive code in C++, so… let’s write the most beautiful pieces of code that prints 42!

Send in a beautiful piece of C++ code that prints 42, and we’ll celebrate Towel Day with it.

What do you mean by “beautiful code”?

We’re talking code art here, so no hard rule. For example, you can use a library that you find helps writing nice code in general. For example the range-v3 library:

#include <iostream>
#include <range/v3/view/iota.hpp>
#include <range/v3/view/take_exactly.hpp>

int main()
{
    std::cout << (ranges::view::ints | ranges::view::take_exactly(42)).size();
}

Or you could design a piece of code around its layout:

#include <iostream>

int main()
{
    int i = -8;
    i +=                 1    +       1 + 1;
    i +=             1 + 1    +   1 + 1 + 1 + 1;
    i +=         1 + 1 + 1    +   1 +     1 + 1;
    i +=     1 + 1     + 1    +          1 + 1;
    i +=     1         + 1    +         1 + 1;
    i +=     1 + 1 + 1 + 1    +        1 + 1;
    i +=     1 + 1 + 1 + 1    +       1 + 1;
    i +=             1 + 1    +      1 + 1;
    i +=             1 + 1    +     1 + 1;
    i +=             1 + 1    +   1 + 1 + 1 + 1;
    
    std::cout << i << '\n';
}

Or you could use template metaprogramming if you’re a template metaprogramming person. Or you could do something even different. Be creative. If you find it beautiful, someone else in the Universe will do too!

How is this going to work?

Today is the 11th of May, and Towel Day is two weeks out, on the 25th of May. You can send in your beautiful program that prints 42 by the 22th of May (included). I will put together all the pieces of code into an exhibit post, and publish your programs on Fluent C++ on Friday the 25th of May, in celebration of Towel Day.

This is not a contest, as in there won’t be one winner. Every piece of C++ code that you send to me and that prints out 42 will published on Fluent C++ that day, with your name next to it.

You can either post a Coliru link in the comments section below, or send me your code directly by email. Just be sure to include your name (or nickname) along with it, so that it’s displayed next to your piece.

Where to read the books

If you haven’t read the books yet, you want to give them a chance!

The 5 volumes are:

And here is an edition that contains all five (that’s the one I have at home):

hitchhiker's guide to the galaxy 42 C++Hoping you’ll get as much fun to read them as I do.

Happy reading, happy coding!

Don't want to miss out ? Follow:   twitterlinkedinrss
Share this post!Facebooktwitterlinkedin

Comments are closed