Jonathan Boccara's blog

Beautiful Pieces of Code That Print 42 for Towel Day

Published May 25, 2018 - 3 Comments

Today is Towel Day!!

This is the day where we celebrates the iconic piece of geek culture: the Hitchhiker Guide to the Galaxy.

In the Hitchhiker Guide to the Galaxy, the number 42 has a central role: it is no less than the Answer to the Ultimate Question of Life, the Universe, and Everything. This is where the 42s in so many code snippets comes from. But there is much more to the story than that, and I recommend you take a look at those books.

To celebrate Towel Day on Fluent C++, everyone had about two weeks to send in their most beautiful piece of code that print 42. And you define beautiful. Here is how the event worked, and special thanks Simon Brand for a fair contribution to the idea.

Quite a few of you participated, and I can’t express how grateful I am for that. You’ve sent in funny, elaborate, suprising… in a word, awesome pieces of code for the event. You guys rock and you’ve made an amazing celebration.

Now is the time to expose those beautiful pieces of code that you wrote. Let’s begin the exhibit featuring the most incredible programs that print 42!

towel day

42 characters

The opening piece, which was the first submission that came in in comments, is from Philipp, with a source code of 42 characters, that prints 42:

#include <cstdio>
int main(){puts("42");}

Output:

42

Picking out Scrabble letters

Next up, we have a pretty mind-blowing piece of code from jedwardsol that you need a little context from the book to understand. The super-computer that worked out the answer 42 cannot tell what the question was. This is why mice (yes) built another even more powerful computer that is often mistaken for a planet because of its shape and biology: this is the Earth.

Towards the end of the second book of the series, The Restaurant at the End of the Universe, Arthur Dent, one of the main characters of the story, somehow ends up in prehistoric Earth. Some of the primitive inhabitants there build a primitive scrabble game without really understanding what they’re doing.

Arthur pulls out some scrabble letters out the bag, and together they form the output of this program:

#include <iostream>
#include <string>
#include <random>
#include <algorithm>
#include <thread>
#include <chrono>

using namespace std::string_literals;
using namespace std::chrono_literals;

template <typename T>
struct seed_seq_emulator
{
    seed_seq_emulator(std::random_device &rd) : rd{rd} {}

    template <typename U>
    void generate(U begin, U end)
    {
        for(auto seed=begin;seed<end;seed++)
        {
            *seed=rd();
        }
    }

private:
    std::random_device &rd;
};


template <typename C, typename RNG>
void FYshuffle(C &container, RNG &rng)
{
    for(auto i=container.size()-1;i>0;i--)
    {
        std::swap(container[i],container[rng()%(i+1)]);
    }
}

template <typename RNG>
void seed(RNG rng)
{
    std::random_device rd;
    seed_seq_emulator<std::mt19937> seeder{rd};   
    rng.seed(seeder);
}
        
int main(void)
{
    std::mt19937 rng;
    // Mersenne twister has 2Kb of state.   
    // std::mt19937  rng{rd()};  only randomises 4 bytes of that state.
    // So do it properly :-
    seed(rng);    

    // A scrabble set contains A*9 B*2 C*2 D*4 E1*2 F*2 G*3 H*2 I*9 J*1 K*1 L*4 M*2 N*6 O*8 P*2 Q*1 R*6 S*4 T*6 U*4 V*2 W*2 X*1 Y*2 Z*1
    auto scrabbleBag="__AAAAAAAAABBCCDDDDEEEEEEEEEEEEFFGGGHHIIIIIIIIIJKLLLLMMNNNNNNOOOOOOOOPPQRRRRRRSSSSTTTTTTUUUUVVWWXYYZ"s;
    // shuffle properly giving, for example ,
    auto scrambledBag{"YSE_WXIOIVAUEQOEHOOYENITSDAAYCIBPTAZREMJNDEEOGAESGOTIRFULIRDHRUICUNFWLRADNBTAETORALPLTISYNOMKEGY_WAN"s};
    FYshuffle (scrambledBag,rng);
    std::for_each(std::begin(scrambledBag),std::begin(scrambledBag)+46,[](auto c){std::cout << c << std::flush; std::this_thread::sleep_for(100ms);});
    std::cout << '\n';
}

Output:

WHATDOYOUGETWHENYOUMULTIPLYSIXBYNINE_FORTY_TWO

Nicely done.

H2G2

The Hitchhiker Guide to the Galaxy, is often abbreviated as H2G2 (letters in bold). This is what Joe Davis takes advantage of to print 42:

#include <iostream>

int main(void)
{
    std::cout << (6 * '9') % ('h'*'2'-'g'*'2') << std::endl;
}

Output:

42

Straight to the point

Beautiful code gets straight to the point for Pål Brønlund:

#include <iostream>

int main() {
    std::cout << "42" << std::endl;
    return 0;
}

Output:

42

Coercing the file API

Roman Sztergbaum takes us to the file stream API which ends up sending a 42 to the console:

#include <cstdio>
#include <unistd.h>

int main()
{
    return fwrite("42", sizeof(char), sizeof("42"), stdout);
}

Output:

42

The matrix

vlovo thinks it must have to do with the matrix, and displays a beautiful 42 of stars in the console output:

#include <algorithm>
#include <iostream>
#include <vector>

// (c) by vlovo,  MLeitz at boptics.de

namespace HG2G
{

using DentMatrixRow = std::vector<int>;
using DentMatrix = std::vector<DentMatrixRow>;

template <class... Args> DentMatrix theAnswer(const Args &... args)
{
  std::vector<DentMatrix> data{args...};

  const size_t rows = data[0].size();
  DentMatrix answer = DentMatrix(rows);

  for (size_t row = 0; row < rows; ++row)
  {

    std::for_each(begin(data), end(data), [&](const DentMatrix &matrix) {
      answer[row].insert(end(answer[row]), begin(matrix[row]), end(matrix[row]));
    });
  }

  return (answer);
}

std::ostream &operator<<(std::ostream &os, const DentMatrix &matrix)
{

  std::for_each(begin(matrix), end(matrix),
                [&](const DentMatrixRow &row) {
                  std::for_each(begin(row), end(row), [&os](const int &element) {
                    if (42 == element)
                    {
                      os << "* ";
                    }
                    else
                    {
                      os << "  ";
                    }
                  });
                  os << "\n";
                }

  );

  return os;
}

} 

int main()
{
  using namespace HG2G;

  DentMatrix four = {{42, 0, 0, 0, 0}, {42, 0, 0, 0, 0}, {42, 0, 0, 42, 0}, {42, 42, 42, 42, 42},
                     {0, 0, 0, 42, 0}, {0, 0, 0, 42, 0}, {0, 0, 0, 42, 0}};

  DentMatrix two = {{0, 42, 42, 42, 0}, {42, 0, 0, 0, 42}, {0, 0, 0, 0, 42},    {0, 42, 42, 42, 0},
                    {42, 0, 0, 0, 0},   {42, 0, 0, 0, 0},  {42, 42, 42, 42, 42}};

  std::cout << theAnswer(four, two);

  return 0;
}

Output:

*           * * *   
*         *       * 
*     *           * 
* * * * *   * * *   
      *   *         
      *   *         
      *   * * * * *

Deep Thought’s source code?

Sean gives us the code that was in Deep Thought (the computer that got to the answer 42 in the first place). As you can imagine, I haven’t finished running the program yet, but I strongly believe that it will eventually print 42.

#include <iostream>
#include <chrono>
#include <thread>

int main() {
    using namespace std::this_thread;
    using namespace std::chrono;
    
    int year=0;
    for (;year<7500000;++year)
        sleep_for(seconds(31557600));
        
    std::cout<<42;
}

Output:

42, most probably.

Taking advantage of corruptions

Here is another version of Deep Thought’s source code by Charles Wilcox, relying on memory corruptions that should inevitably happen:

#include <cstdlib> // EXIT_SUCCESS, EXIT_FAILURE
#include <iostream> // cout, clog, flush
#include <exception> // exception

enum Things
{
    Life = 0x20,
    The_Universe = 010,
    Everything = 0b10
};

class Deep_Thought
{
public:
    template< typename... Args >
    auto what_is_the_answer_to_the_great_question( Args... args )
    {
        m_computing = true;
        while( m_computing )
        {}
        auto the_answer = ( ... | args );
        m_checking = true;
        while( m_checking )
        {}
        return the_answer;

        // Google data-centers' observed DRAM corruption error rates 
        //   of "25,000 to 70,000 errors per billion device hours per Mbit",
        //   which is 1 bit per 1.7 to 4.8 million years.
        //   Two of those is close enough to the 7.5 million years in the story; YMMV.
        //   ( 25000 / (1000^3 / 24 / 365.2425) / 1024^2 )^-1 ~= 4.785 million years per bit error
        //   ( 70000 / (1000^3 / 24 / 365.2425) / 1024^2 )^-1 ~= 1.709 million years per bit error
        //     This assumes 'Mbit' is a 'mebibit', of course.
        // http://www.cs.toronto.edu/~bianca/papers/sigmetrics09.pdf
    }
private:
    volatile bool m_computing :1;
    bool :0;
    volatile bool m_checking :1;
};

class Mice
{
public:
    Deep_Thought make_computer()
    { return {}; }
};
Mice g_mice;


int main( int argc, char const* argv[] )
try
{
    auto deep_thought = g_mice.make_computer();
    std::cout
      << deep_thought.what_is_the_answer_to_the_great_question( Life,
                                                                The_Universe,
                                                                Everything )
      << '\n';
    return EXIT_SUCCESS;
}
catch( std::exception const& std_ex )
{
    std::clog << argv[ 0 ] << ": detected a standard error: \""
              << std_ex.what() << "\".  Exiting.\n" << std::flush;
    return EXIT_FAILURE;
}
catch( ... )
{
    std::clog << argv[ 0 ] << ": detected an unrecognized error; "
              << "ask the Vogons.  Exiting.\n" << std::flush;
    return EXIT_FAILURE;
}

Output: 42 at some point, if all goes well. Or rather, if not all goes well.

4s, 2s, and 42s

An brilliant mix of 4s, 2s, and 42s in code and in output, by Andy Salerno, sent in by email!

Note that “DON’T PANIC” is an ubiquitous phrase in the Hitchhiker book series. Indeed, the Hitchhiker Guide to the Galaxy is also a book about a book called the Hitchhiker Guide to the Galaxy that contains a myriad of practical tips for those who want to travel the Galaxy at a reasonable price (less than thirty Altairian dollars a day, to be precise). And this book displays “DON’T PANIC” on its cover, written in large and friendly letters.

#include <iostream>

int main()
{
    const char* dont_panic = "DON'T PANIC!";

    auto D = *((int*)dont_panic + (42 >> 4 * 2));
    auto N = *((int*)dont_panic + (42 / 42));
    auto A = *((int*)dont_panic + (4 - 2));

    for (int x = 42; x < 42 + (4 + 2 - (42 / 42)); x++) {
        D /= 42;
        N /= 42;
        A /= 42;
    }

    A -= 4 - 2;

    std::cout << (D * N) + A << std::endl;
}

Output:

42

Dissecting the question

42 is the answer of Deep Thought to the Ultimate Question of Life, the Universe, and Everything. This is exactly what the code of James M. does. But will you understand the inner workings of Deep Thought?

#include <iostream>

/* 42 */

int main()
{

	struct DeepThought {
		static int answer(const char *question) {
			int solution = 0;
			while(*question) solution ^= *question++;
			return ++solution;
			}
	};

	std::cout << 
		DeepThought::answer("The answer to the Ultimate Question of Life, the Universe, and Everything") 
		 << std::endl;
}

Output:

42

The question they want us to believe

Here is a piece of code by James Moran that shows what the question was – at least the one the mice want to make us think it was:

#include "stdio.h"

#define SIX 1 + 5
#define NINE 8 + 1

int main(void)
{
  printf("%d\n", SIX * NINE);

  return 0;
}

Output:

42

42 lines of code

A program made by Aman Saxena, that’s 42 lines to display 42! Note the importance of the exact number of 42 lines to display the Answer.

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

class TowelDay
{
private:
    ifstream file;

public:
    TowelDay(const string& str) : file(str)
    {

    }

    auto get_forty_two()
    {
        auto count = 0;
        char a[80];

        while(!file.eof())
        {
            file.getline(a,sizeof(a));
            count++;
        }

        return count;
    }

};

int main()
{
    TowelDay towelDay(__FILE__);
    
    auto count = towelDay.get_forty_two();
    cout<<count<<endl;

    return 0;
}

Output:

42

Abusing assembler compatibility

To quote Shafik Yaghmour’s tweet, “this is evil and undefined behavior but since this is all about fun:”

const char main[] ="\xb8\x2a\x00\x00\x00\xc3";

The program returns 42. How does this works? These are hexadecimal codes corresponding to assembler instructions of a program that just returns 42!

Artistic code

An piece of code pleasant to the eye proposed by Reddit user /r/drjeats. Notice the effort to fit exactly 42 ‘1’s in it:

#include <iostream>
 
int main()
{
int i = 0;
i +=             1      +     1 + 1 + 1;
i +=         1 + 1      +   1 +     1 + 1;
i +=       1   + 1      +          1 + 1;
i +=     1     + 1      +         1 + 1;
i +=     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';
}

Output:

42

Indeed, the example in the challenge post showed perhaps more regular letters, but didn’t quite make the right numbers of ‘1’s:

#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';
}

More artistic code

Still in an artistic style, behold this beautiful representation of 42 by Reddit user /r/d-frey that also prints a 42!

#include <iostream>

int main()
{
    int fourtytwo{};
    std::cout << ++

    ++  ++   ++++++
    ++  ++   ++++++
    ++  ++       ++
    ++  ++       ++
    ++++++   ++++++
    ++++++   ++++++
        ++   ++
        ++   ++
        ++   ++++++
        ++   ++++++

    ++ fourtytwo <<
    std::endl;
}

Output:

42

Deriving 42 from zero

An interesting derivation of 42 from literally zero by Reddit user /r/JavaSuck:

#include <iostream>

int main()
{
    std::cout << sizeof(0) << sizeof("0") << '\n';
}

Output:

42

Seeing the kids

Reddit user /r/renrutal explains that “this is the most beautiful piece of code that prints 42 because you don’t spend time wondering what’s going on, and you can go earlier home to see your beautiful kids, and anyone who truly matters to you”. A lesson of lifestyle!

#include <iostream>

int main()
{
    std::cout << "42\n";
}

Output:

42

Blindfolded shooting

A nice humorous algorithm from Reddit user /r/neoform:

#include <iostream>
#include <stdlib.h>

int main()
{
    int i;
    while (1) {
        i = rand();
        if (i == 42) {
            std::cout << "42\n";
            break;
        }
    }
}

Output: hasn’t come out yet, but I have hope that a 42 will come out eventually. With time, and a tiny bit of luck.

Incrementing zero

To understand this one, its author Zoltán Vajda comments that “the idea was to create 42 by incrementing zero several times, where neither the increment, nor the numer of incrementations are 42”. As a side note, the author adds that “this, of course, makes no sense, but the whole challenge makes no sense, therefore this should be fine.” 🙂

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>

using uc_pair_t = std::pair<unsigned char, unsigned char>;

std::vector<uc_pair_t> some_pairs = {
    {2, 21},
    {3, 14},
    {5, 162},
    {6, 7},
    {7, 6},
    {9, 90},
    {10, 81},
    {11, 190},
    {13, 82},
    {14, 3},
    {15, 54},
    {17, 138},
    {18, 45},
    {19, 110},
    {21, 2},
    {22, 95},
    {23, 102},
    {25, 186},
    {26, 41},
    {27, 30},
    {29, 178},
    {30, 27},
    {31, 150},
    {33, 234},
    {34, 69},
    {35, 206},
    {37, 98},
    {38, 55},
    {39, 198},
    {41, 26},
    {43, 126},
    {45, 18},
    {46, 51},
    {47, 246},
    {49, 74},
    {50, 93},
    {51, 46},
    {53, 194},
    {54, 15},
    {55, 38},
    {57, 122},
    {58, 89},
    {59, 222},
    {61, 114},
    {62, 75},
    {63, 86},
    {65, 170},
    {66, 117},
    {67, 142},
    {69, 34},
    {70, 103},
    {71, 134},
    {73, 218},
    {74, 49},
    {75, 62},
    {77, 210},
    {78, 99},
    {79, 182},
    {81, 10},
    {82, 13},
    {83, 238},
    {85, 130},
    {86, 63},
    {87, 230},
    {89, 58},
    {90, 9},
    {91, 158},
    {93, 50},
    {94, 123},
    {95, 22},
    {97, 106},
    {98, 37},
    {99, 78},
    {101, 226},
    {102, 23},
    {103, 70},
    {105, 154},
    {106, 97},
    {107, 254},
    {109, 146},
    {110, 19},
    {111, 118},
    {113, 202},
    {114, 61},
    {115, 174},
    {117, 66},
    {118, 111},
    {119, 166},
    {121, 250},
    {122, 57},
    {123, 94},
    {125, 242},
    {126, 43},
    {127, 214},
    {130, 85},
    {131, 14},
    {133, 162},
    {134, 71},
    {135, 6},
    {137, 90},
    {138, 17},
    {139, 190},
    {141, 82},
    {142, 67},
    {143, 54},
    {145, 138},
    {146, 109},
    {147, 110},
    {149, 2},
    {150, 31},
    {151, 102},
    {153, 186},
    {154, 105},
    {155, 30},
    {157, 178},
    {158, 91},
    {159, 150},
    {161, 234},
    {162, 5},
    {163, 206},
    {165, 98},
    {166, 119},
    {167, 198},
    {169, 26},
    {170, 65},
    {171, 126},
    {173, 18},
    {174, 115},
    {175, 246},
    {177, 74},
    {178, 29},
    {179, 46},
    {181, 194},
    {182, 79},
    {183, 38},
    {185, 122},
    {186, 25},
    {187, 222},
    {189, 114},
    {190, 11},
    {191, 86},
    {193, 170},
    {194, 53},
    {195, 142},
    {197, 34},
    {198, 39},
    {199, 134},
    {201, 218},
    {202, 113},
    {203, 62},
    {205, 210},
    {206, 35},
    {207, 182},
    {209, 10},
    {210, 77},
    {211, 238},
    {213, 130},
    {214, 127},
    {215, 230},
    {217, 58},
    {218, 73},
    {219, 158},
    {221, 50},
    {222, 59},
    {223, 22},
    {225, 106},
    {226, 101},
    {227, 78},
    {229, 226},
    {230, 87},
    {231, 70},
    {233, 154},
    {234, 33},
    {235, 254},
    {237, 146},
    {238, 83},
    {239, 118},
    {241, 202},
    {242, 125},
    {243, 174},
    {245, 66},
    {246, 47},
    {247, 166},
    {249, 250},
    {250, 121},
    {251, 94},
    {253, 242},
    {254, 107},
    {255, 214}
};

auto do_the_magic = [](const uc_pair_t &uc_pair){
    unsigned char value = 0;
    unsigned char increment = uc_pair.first;
    unsigned char counter = uc_pair.second;
    while (counter != 0) {
        value += increment;
        counter--;
    }
    std::cout << +value << std::endl;
};

int main() {
    std::for_each(some_pairs.cbegin(), some_pairs.cend(), do_the_magic);
    return 0;
}

Output:

42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42
42

Ben’s algorithms

Ben Deane used this Towel Day event as an opportunity to write a bunch of nice algorithms, that end up printing the desired 42. Algorithms are cool indeed.

#include <algorithm>
#include <cassert>
#include <iostream>
#include <iterator>
#include <numeric>
#include <utility>
#include <vector>

//------------------------------------------------------------------------------
// Towel day 2018!
// https://www.fluentcpp.com/2018/05/11/the-most-beautiful-piece-of-code-that-prints-42/
//
// We're going to print the number "42" in a roundabout way.
// With some algorithms. We like algorithms. Algorithms are cool.
//------------------------------------------------------------------------------

// distribute
//
// Given a start value and a bucket size n, take the sum of the start value and
// the values [first, last), and distribute (bucket) the sum by n's.
//
template <typename T, typename ForwardIt, typename OutputIt>
OutputIt distribute(T start, T n,
                    ForwardIt first, ForwardIt last,
                    OutputIt dest)
{
  // sum what's left
  auto sum = std::accumulate(first, last, start);
  // distribute it in buckets of n
  auto q = sum / n;
  dest = std::fill_n(dest, q, n);
  // account for the remainder, if any
  auto r = sum % n;
  if (r > 0) *dest++ = r;
  return dest;
}

// next_partition
//
// Given an input range [first, last) representing an integer partition,
// output the next partition (lexicographically) to dest.
//
// Return {dest, true} when a partition was written to dest.
// Return {dest, false} when there is no next partition.
//
template <typename ForwardIt, typename OutputIt>
std::pair<OutputIt, bool> next_partition(ForwardIt first, ForwardIt last,
                                         OutputIt dest)
{
  // empty range, or already all ones (the last partition)
  if (first == last || *first == 1) return {dest, false};

  // copy until the next value is one (or no next value)
  auto next = std::next(first);
  for (; next != last && *next != 1; ++next)
  {
    *dest++ = *first++;
  }

  // decrement the value, distribute the remainder
  auto n = *first - 1;
  *dest++ = n;
  return {distribute(1, n, next, last, dest), true};
}

// prev_partition
//
// Given an input range [first, last) representing an integer partition,
// output the previous partition (lexicographically) to dest.
//
// Return {dest, true} when a partition was written to dest.
// Return {dest, false} when there is no previous partition.
//
template <typename BidirIt, typename OutputIt>
std::pair<OutputIt, bool> prev_partition(BidirIt first, BidirIt last,
                                         OutputIt dest)
{
  // empty range, or single number (the first partition)
  if (std::distance(first, last) <= 1) return {dest, false};

  // take the number off the end
  auto prev = std::prev(last);

  // find the last number that will not become bigger than its predecessor when
  // we add one to it
  auto it = std::adjacent_find(
      std::make_reverse_iterator(prev),
      std::make_reverse_iterator(first),
      [] (auto x, auto y) { return x < y; });
  auto pos = it.base();
  if (pos != first) --pos;

  // copy up to there
  dest = std::copy(first, pos, dest);
  // add one at that position and distribute the remainder as ones
  *dest++ = *pos++ + 1;
  return { distribute(-1, 1, pos, last, dest), true };
}

// replace_iterator
//
// An iterator that replaces the values in its container, calling push_back as
// necessary. This is handy for in-place computation of integer partitions,
// since the output size may differ from the input range size.
//
// This iterator isn't used in the final program, but I did use it while
// developing.
//
template <typename Container>
struct replace_iterator
{
  using container_type = Container;
  using base_iterator = typename Container::iterator;
  using const_base_iterator = typename Container::const_iterator;
  using base_value_type = typename Container::value_type;

  using value_type = void;
  using difference_type = void;
  using pointer = void;
  using reference = void;
  using iterator_category = std::output_iterator_tag;

  replace_iterator(Container& c) : c(&c), i(std::begin(c)) {}

  replace_iterator& operator=(const base_value_type& v) { return assign(v); }
  replace_iterator& operator=(base_value_type&& v) { return assign(std::move(v)); }

  replace_iterator& operator*() { return *this; }
  replace_iterator& operator++() { return *this; }
  replace_iterator& operator++(int) { return *this; }

  base_iterator base() { return i; }
  const_base_iterator base() const { return i; }

private:
  template <typename V>
  replace_iterator& assign(V&& v)
  {
    if (i == std::end(*c))
    {
      c->push_back(std::forward<V>(v));
      i = std::end(*c);
    }
    else
    {
      *i = std::forward<V>(v);
      ++i;
    }
    return *this;
  }

  Container* c;
  base_iterator i;
};

// iterate_integer_partitions
//
// Given an integer n, return a function that will iterate through the integer
// partitions of n, returning successive partitions as vector<int>. When the
// partitions are exhausted, the vector returned is empty.
//
// The integer partitions of n are the distinct ways of writing n as a sum of
// positive integers less than n. Integer partitions have a natural
// lexicographical ordering. For example, the 11 integer partitions of 6 are:
// {6}, {5,1}, {4,2}, {4,1,1}, {3,3}, {3,2,1}, {3,1,1,1}, {2,2,2}, {2,2,1,1},
// {2,1,1,1,1}, {1,1,1,1,1,1}
//
auto iterate_integer_partitions(int n)
{
  return [v = std::vector{n}] () mutable {
    auto ret = std::exchange(v, {});
    next_partition(ret.cbegin(), ret.cend(),
                   std::back_inserter(v));
    return ret;
  };
}

int main()
{
  // Test: there are 627 integer partitions of 20.
  // http://www.wolframalpha.com/input/?i=partitions+of+20
  {
    auto f = iterate_integer_partitions(20);
    int n = 0;
    while(!f().empty()) ++n;
    assert(n == 627);
  }

  // There are 42 integer partitions of 10.
  auto f = iterate_integer_partitions(10);
  int n = 0;
  while(!f().empty()) ++n;
  std::cout << n << '\n';
}

Output:

42

Bitsets

Here is an elaborate manipulation of bitsets, by Eric Albright:

#include <bitset>
#include <iterator>
#include <utility>
#include <limits>
#include <type_traits>
#include <iostream>
 
template<size_t Bits>
class bitset_iterator
{
public:
    using iterator_category = std::input_iterator_tag;
    static_assert(Bits <= std::numeric_limits<uint64_t>::digits, "bitset_iterator cannot use more than 64 bits");
    using value_type = std::conditional_t<Bits <= std::numeric_limits<uint32_t>::digits, uint32_t, uint64_t>;
    using difference_type = ptrdiff_t;
    using pointer = const value_type *;
    using reference = const value_type &;
 
    using bitset_type = std::bitset<Bits>;
 
private:
    bitset_type* bitset;
    int index;
    value_type current_value;
 
    struct end_iterator{};
    constexpr bitset_iterator(bitset_type& bitset, end_iterator)
        : bitset(std::addressof(bitset))
        , index(-1)
    {
    }
 
public:
    constexpr static bitset_iterator end (bitset_type& bitset)
    {
        return bitset_iterator(bitset, end_iterator());
    }
 
    constexpr bitset_iterator(bitset_type& bitset)
        : bitset(std::addressof(bitset))
        , index(static_cast<int>(bitset.size()))
    {
        move_next();
    }
 
    constexpr reference operator*() const
    {
        return current_value;
    }
 
    constexpr pointer operator->() const
    {
        return std::addressof(current_value);
    }
 
    constexpr bitset_iterator& operator++() // preincrement
    {
        move_next();
        return *this;
    }
 
    constexpr bitset_iterator operator++(int) // postincrement
    {
        auto current_value = *this;
        ++*this;
        return current_value;
    }
 
    constexpr bool operator==(const bitset_iterator& other) const
    {
        return other.bitset == bitset
            && other.index == index;
    }
 
private:
    constexpr void move_next()
    {
        for (--index; index > -1; --index)
        {
            if (index > 0 && bitset->test(index))
            {
                current_value = value_type(1) << index;
                break;
            }
        }
    }
};
 
 
template<typename Iterator>
class virtual_range
{
    Iterator first;
    Iterator last;
public:
    constexpr virtual_range(Iterator first, Iterator last)
        : first(first)
        , last(last)
    {
    }
 
    constexpr Iterator begin() const
    {
        return first;
    }
 
    constexpr Iterator end() const
    {
        return last;
    }
};
 
template<size_t Bits>
constexpr auto iterate_over(std::bitset<Bits>& bitset)
{
    return virtual_range<bitset_iterator<Bits>>(bitset_iterator<Bits>(bitset), bitset_iterator<Bits>::end(bitset));
}
 
int main()
{
    using namespace std::rel_ops;
 
    std::bitset<42> the_answer(0b0110);
    for (auto i : iterate_over(the_answer))
    {
        std::cout << i;
    }
    std::cout << std::endl;
    return 0;
}

Output:

42

Intriguing binary representation

Here is an astute piece of template code by xffox that exploits the fact that 42 happens to be 101010 in binary:

#include <iostream>
#include <utility>

namespace
{
    // Compile-time answer to the Ultimate Question of Life, the Universe, and
    // Everything. Use that 42 is nicely 101010 in binary.
    class deep_thought
    {
    public:
        static constexpr unsigned int answer()
        {
            return make_answer(
                    std::make_integer_sequence<unsigned int, 3>());
        }

    private:
        deep_thought() = default;

        template<unsigned int... Is>
        static constexpr unsigned int make_answer(
                std::integer_sequence<unsigned int, Is...>)
        {
            return ((1<<(2*Is+1)) | ...);
        }
    };
}

int main()
{
    static_assert(deep_thought::answer() == 42);
    std::cout<<deep_thought::answer()<<std::endl;
    return 0;
}

Output:

42

{fmt}

Here is how to print 42 with the popular fmt library, submitted by its author himself, Victor Zverovitch:

#include <fmt/core.h>

int main() {
  fmt::print("{}", 42);
}

Output:

42

Fibonacci

Sören Schellhoff uses a subtle combination of fibonacci numbers and arithmetic operations, to land on the expected Answer:

/*
author Sören Schellhoff
some description:
I use five fibonacci numbers and the operators +, -, * and / to calculate the output value.
Ints are lame, so i made two constants ZERO and ONE and a function successor(int) to create the parameters for the fibonacci function.
Had been cooler to build some number class with peano-number like functions for + and * and comparable versions for - and /. Maybe next time ;)
So long and thanks for all the fish.
*/

#include <iostream>

const int ZERO = 'a' - 'a';
const int ONE = 'b' - 'a';

int successor(int number) {
  return number + ONE;
}

int fibonacci(unsigned int no) {
  if(no == ZERO) {
    return ZERO;
  }
  int a = ZERO;
  int b = ONE;
  for(;no > ONE; no--) {
    int temp = a + b;
    a = b;
    b = temp;
  }
  return b;
}

int main(int argc, char *argv[]) {
  std::cout << ((((fibonacci(successor(successor(successor(successor(successor(successor(successor(successor(successor(successor(successor(successor(ZERO))))))))))))) + fibonacci(successor(successor(ZERO)))) / fibonacci(successor(successor(successor(successor(successor(ZERO))))))) - fibonacci(successor(successor(successor(successor(successor(successor(ZERO)))))))) * fibonacci(successor(successor(successor(ZERO))))) << std::endl;
  return 0;
}

Output:

42

Cling

Twitter user @PreeJackie feeds the following piece of code to Cling, the interactive C++ interpreter, to output 42 in its REPL:

std::cout<<"42"

Carved out 42

Here is an elaborate piece of code by Emanuele Bonin from Italy, that prints a just as elaborate 42:

#include <iostream>
#define FortyTwo  42
#define FortyTwoOverTwo (FortyTwo/2)
#define FortyTwo_  'X'
#define FortyTwo__ ' '
using namespace std;
int main() {
    char c42 = FortyTwo_;
    int fOrtyTwo, foRtyTwo, forTyTwo, fortYTwo;
    unsigned long long fortyTwo;
    unsigned long long fortytwo[FortyTwoOverTwo]
        = {0,0,
           15030909,18021008,
           19021107,70507021206,
           71406010605,71406020604,
           91106030603,12041802,
           12041802,11011802,
           7020612,7020612,
           60707020612,3110,
           3110,0,
           0,0
          };

    for(int FortyTwoCounter=0; FortyTwoCounter < FortyTwoOverTwo; FortyTwoCounter++) {
        forTyTwo = 1;
        fOrtyTwo = 0;
        fortyTwo = fortytwo[FortyTwoCounter];
        while(fOrtyTwo < FortyTwo) {
            foRtyTwo = fortyTwo % 10 + (fortyTwo / 10 % 10) * 10;
            fortyTwo= fortyTwo/100;
            fortYTwo = 0;
            while (foRtyTwo > 0 && fortYTwo == 0) {
                cout << c42;
                fOrtyTwo++;
                if(++forTyTwo > foRtyTwo) {
                    c42 = (c42 == FortyTwo_? FortyTwo__: FortyTwo_);
                    fortYTwo = 1;
                    forTyTwo = 1;
                }
            }
            if(foRtyTwo == 0) {
                while(fOrtyTwo <= FortyTwo) {
                    fOrtyTwo++;
                    cout << c42;
                }
                cout << endl;
            }
        }

    }
}

Output:

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXX         XXX               XXXXXXX
XXXXXXXX          XX                  XXXXX
XXXXXXX           XX                   XXXX
XXXXXX            XX       XXXXX       XXXX
XXXXX      X      XXXXXXXXXXXXXX       XXXX
XXXX      XX      XXXXXXXXXXXXXX       XXXX
XXX      XXX      XXXXXXXXXXX         XXXXX
XX                  XXXX            XXXXXXX
XX                  XXXX            XXXXXXX
XX                  X           XXXXXXXXXXX
XXXXXXXXXXXX      XX       XXXXXXXXXXXXXXXX
XXXXXXXXXXXX      XX       XXXXXXXXXXXXXXXX
XXXXXXXXXXXX      XX       XXXXXXX      XXX
XXXXXXXXXX                               XX
XXXXXXXXXX                               XX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The answer and the question

This program by Kevin Flitch should eventually produce 42, along with the question that led to the Answer:

#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <map>

class TheEarth {
  public:
    TheEarth() : bag_of_scrabble_letters {
        {87,63},{43,104},{65,117},{51,111},{47,100},{76,115},{64,111},{68,117},
        {86,101},{70,116},{58,119},{66,32},{79,32},{50,121},{78,120},{85,110},
        {45,116},{55,101},{60,101},{46,32},{42,87},{61,110},{82,32},{63,121},
        {84,105},{83,110},{74,121},{56,116},{62,32},{75,32},{49,32},{80,98},
        {53,32},{69,108},{71,105},{52,117},{73,108},{57,32},{88,0},{81,121},
        {54,103},{67,109},{48,111},{72,112},{44,97},{59,104},{77,105}}{}

    std::string question() {
        std::string question;
        for (auto [ _, c] : bag_of_scrabble_letters) {
            question += c;
        }
        return question;
    }
  private:
    std::map<int,char> bag_of_scrabble_letters;
};

class DeepThought {
  public:
    int answer() const {
        std::this_thread::sleep_for(std::chrono::hours(24*365*7500000));
        return 42;
    }

    TheEarth question_computer () const {
        return TheEarth{};
    }
};

int main()
{
    DeepThought deep_thought{};
    std::cout << "The answer is: "<< deep_thought.answer() << '\n';
    std::cout << "The question is: "<< deep_thought.question_computer().question() << '\n';
}

…but it hasn’t finished running yet on my machine.

It was so simple

Miroslav Franc sent a short and sweet piece of code that says it all:

#include <iostream>

struct answer { void operator()(int i) {std::cout << i << '\n'; } };

#define six            4 * 2
#define by           * 4 | 2 *
#define nine           4 ^ 2
#define multiply
#define universe int           main

universe() { answer()(multiply six by nine); }

Output:

42

The question?

Bartosz Borowik’s code shows that it could be a pretty straightforward question after all:

#include <iostream>
#include <string>

auto deep_thought(std::string command)
{
	return command.length();
}

int main()
{
    std::cout << std::hex << deep_thought("Answer the Ultimate Question of Life, the Universe, and Everything") << '\n';
}

Output:

42

Bit counting

In this second submission from Bartosz, the Answer comes from some bit counting:

#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/date_time/gregorian/conversion.hpp"
#include <iostream>
#include <bitset>

std::int64_t to_timestamp(const boost::gregorian::date &date)
{
    auto tm = boost::gregorian::to_tm(date);
    return mktime(&tm);
}

int main()
{
    const auto towel_day = boost::gregorian::date(2018, 5, 25);
    
    const auto towel_day_ts = to_timestamp(towel_day);
	const auto bits = std::bitset<sizeof(towel_day_ts) * CHAR_BIT>(towel_day_ts);
	
	const auto set_bit_count = bits.count();
	const auto unset_bit_count = bits.size() - bits.count();
	
	std::cout << unset_bit_count - set_bit_count << '\n';
}

Output:

42

Coroutines

mattnewport crafted his submission to satisfy the following constraints: no digits in the source, produce 42 implicitly from some somewhat interesting maths, demonstrate how coroutines / generators can be used to do some functional style code in C++ (infinite / lazy sequences with functional style higher order functions).:

#include <algorithm>
#include <iostream>
#include <limits>
#include <numeric>
#include <string>

#include <experimental/generator>

using namespace std;

auto naturals(int n = numeric_limits<int>::max()) {
    for (int i{}; ++i < n;) co_yield i;
}

template <typename R, typename F>
auto filter(R range, F pred) {
    for (auto x : range)
        if (pred(x)) co_yield x;
}

template <typename R, typename F>
auto map(R range, F f) {
    for (auto x : range) co_yield f(x);
}

template <typename R, typename T>
auto sum(R range, T init) {
    return accumulate(begin(range), end(range), init);
}

template <typename R>
auto intersect(R rangeA, R rangeB) {
    for (auto firstA = begin(rangeA), firstB = begin(rangeB);
         firstA != end(rangeA) && firstB != end(rangeB);)
        if (auto a = *firstA, b = *firstB; a < b)
            ++firstA;
        else if (b < a)
            ++firstB;
        else {
            ++firstA, ++firstB;
            co_yield a;
        }
}

auto areParensMatched(const string& s) {
    auto count = int{};
    for (auto c : s)
        if (c == '(')
            ++count;
        else if (c == ')')
            if (!count--) return false;
    return !count;
}

auto permutations(string s) {
    do {
        co_yield s;
    } while (next_permutation(begin(s), end(s)));
}

auto catalan(int n) {
    return sum(map(permutations(string(n, '(') + string(n, ')')), areParensMatched), int{});
}

auto isAbundant(int n) {
    return sum(filter(naturals(n), [n](int x) { return !(n % x); }), int{}) > n;
}

int main() {
    cout << *intersect(filter(naturals(), isAbundant), map(naturals(), catalan)).begin();
}

Output:

42

Compiler directives

John Melas uses compiler specific compiler directives to produce a 42. Not in the console, but in the compiler output:

#define STRINGIZE(X) DO_STRINGIZE(X)
#define DO_STRINGIZE(X) #X

#if __GNUC__ || __clang__
#define COMPILER_MESSAGE(x) _Pragma(STRINGIZE(message(x)))
#elif _MSC_VER
#define COMPILER_MESSAGE(x) __pragma(message(x))
#endif

COMPILER_MESSAGE("42")

int main() {}

Dragon Ball Z

A pretty fun submission from Tony Rivere:

#include <iostream>
#include <tuple>

template<typename ...Args>
auto UniversalAnswer(Args&&...iArgs)
{
    return [iArgs = std::make_tuple(std::forward<Args>(iArgs) ...)] (auto&&... iQuestions) {
        return 42;
    };
}

int main()
{
    auto theAnswer = UniversalAnswer("ka", "me", "ha")("me", "ha");
    std::cout << theAnswer << '\n';
    return theAnswer;
}

Output:

42

K.I.S.S.

Arthur Bowers says: “I’ve read in many, many places (here being one of them I’m pretty sure…!) saying that the most beautiful code is easy to read and understand. […] Here’s my entry! K.I.S.S!”

#include <iostream>


int main()
{
    std::cout << "42!" << std::endl;
    return 42;
}

Output:

42!

TMP

If you like template metaprogramming, you will appreciate Himanshu Tiwari’s submission:

//
//  main.cpp
//  towel day
//
//  Created by Himanshu Tiwari on 20/05/18.
//  Copyright © 2018 Himanshu Tiwari. All rights reserved.
//

#include <iostream>

template<int power>
int PowerUp() {
    return 2*PowerUp<power - 1>();
}

template<>
int PowerUp<1>() {
    return 1;
}

template<int... Args>
void printFoldsum() {
    std::cout << (... + PowerUp<Args>());
}



int main(int argc, const char * argv[]) {
    printFoldsum<4, 2, 4 + 2>();
    return 0;
}

Output:

42

Fibonacci and Haiku

Graeme Williams shows us a Haiku-like for loop leveraging on the Fibonacci sequence to output the Answer:

#include <stdio.h>

int main()
{
    int n = 1;
    for (int m = ++n, i = n + m; i >= n - m; m += n, n += m, ++i);
    printf("%d\n", n);
}

Output:

42

And did you know this curious fact about Modern C++?

Here is an shocking revelation: the Answer to the Ultimate Question to Life, The Universe and Everything is also the Answer to What Do the Modern C++ Versions (C++11, C++14, C++17) Add Up To! What to think of that?

Here is the piece of code using the beautiful range-v3 library to accompany this discovery by Walletfox:

#include <iostream>
#include <vector>
#include <string>
#include <range/v3/all.hpp>
#include <cctype>

// 42 by @walletfox
int main(){ 
    auto onlyDigits =  ranges::view::remove_if(
                           [](const unsigned char c){ return !std::isdigit(c);});
    auto strToInt = [](const std::string& s) -> int {return std::stoi(s);};

    std::vector<std::string> vec_str = {"C++11", "C++14", "C++17"};
    auto answer = ranges::accumulate( 
                        vec_str | ranges::view::transform(onlyDigits)
                                | ranges::view::transform(strToInt),
                        0);
    std::cout << answer << '\n';
}

Output:

42

So long, and thanks for all the submissions

I hope you enjoyed this tribute to Douglas Adams’s Hitchhiker’s Guide to the Galaxy. I find what you guys did fanstastic.

If you also liked it, share it! Show the world that the Ultimate Question of Life, the Universe, and Everything, the question that leads to 42 can take many forms. Show them how the creativity of programmers has no bounds, how beautiful code can take a hundred faces, if only just for printing 42.

And if you haven’t yet, why not have a go at reading the Hitchhiker’s Guide series?

So long, thanks for all the fish and for all submissions, and Happy Towel Day!

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

Comments are closed