Bellissimi pezzi di codice che stampano 42 per il giorno dell'asciugamano

Bellissimi pezzi di codice che stampano 42 per il giorno dell'asciugamano

Oggi è il giorno dell'asciugamano!!

Questo è il giorno in cui celebriamo l'iconico pezzo di cultura geek:la Guida galattica per autostoppisti .

Nella Guida galattica per autostoppisti, il numero 42 ha un ruolo centrale:non è altro che la risposta all'ultima domanda della vita, dell'universo e di tutto. È da qui che vengono i 42 in così tanti frammenti di codice. Ma c'è molto di più nella storia e ti consiglio di dare un'occhiata a quei libri.

Per celebrare il Towel Day su Fluent C++, tutti hanno avuto circa due settimane per inviare il loro pezzo di codice più bello che stampa 42. E tu definisci bello. Ecco come ha funzionato l'evento e un ringraziamento speciale a Simon Brand per il giusto contributo all'idea.

Molti di voi hanno partecipato e non posso esprimere quanto sono grato per questo. Hai inviato divertenti, elaborati, sorprendenti... in una parola, fantastici pezzi di codice per l'evento. Ragazzi rock e avete festeggiato in modo straordinario.

Ora è il momento di esporre quei bellissimi pezzi di codice che hai scritto. Iniziamo la mostra con i programmi più incredibili che stampano 42!

42 caratteri

Il pezzo di apertura, che è stato il primo contributo che è arrivato nei commenti, è di Philipp, con un codice sorgente di 42 caratteri, che ne stampa 42 :

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

Uscita:

42

Scegliere le lettere di Scarabeo

Successivamente, abbiamo un pezzo di codice piuttosto strabiliante di jedwardsol che hai bisogno di un po' di contesto dal libro per capire. Il supercomputer che ha elaborato la risposta 42 non può dire quale fosse la domanda. Per questo i topi (sì) hanno costruito un altro computer ancora più potente che spesso viene scambiato per un pianeta a causa della sua forma e della sua biologia:questa è la Terra.

Verso la fine del secondo libro della serie, The Restaurant at the End of the Universe, Arthur Dent, uno dei personaggi principali della storia, finisce in qualche modo nella Terra preistorica. Alcuni degli abitanti primitivi costruiscono un primitivo gioco di scarabeo senza capire davvero cosa stanno facendo.

Arthur tira fuori dalla borsa alcune lettere di scarabeo e insieme formano l'output di questo programma:

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

Uscita:

WHATDOYOUGETWHENYOUMULTIPLYSIXBYNINE_FORTY_TWO

Ben fatto.

H2G2

La H pruritoh iker G guida al G alaxy, è spesso abbreviato in H2G2 (lettere in grassetto). Questo è ciò che Joe Davis sfrutta per stampare 42:

#include <iostream>

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

Uscita:

42

Dritto al punto

Il bel codice va dritto al punto per Pål Brønlund:

#include <iostream>

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

Uscita:

42

Coercizione dell'API del file

Roman Sztergbaum ci porta all'API del flusso di file che finisce per inviare un 42 alla console:

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

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

Uscita:

42

La matrice

vlovo pensa che abbia a che fare con la matrice e mostra ben 42 stelle nell'output della console:

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

Uscita:

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

Codice sorgente di Deep Thought?

Sean ci fornisce il codice che era in Deep Thought (il computer che è arrivato alla risposta 42 in primo luogo). Come puoi immaginare, non ho ancora finito di eseguire il programma, ma credo fermamente che alla fine ne stamperà 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;
}

Uscita:

42, molto probabilmente.

Sfruttare le corruzioni

Ecco un'altra versione del codice sorgente di Deep Thought di Charles Wilcox, basata su danneggiamenti della memoria che dovrebbero inevitabilmente verificarsi:

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

Uscita:42 ad un certo punto, se tutto va bene. O meglio, se non tutto va bene.

4s, 2s e 42s

Un brillante mix di 4s, 2s e 42s in codice e in output, di Andy Salerno, inviato via email!

Nota che "NON PANIC" è una frase onnipresente nella serie di libri di Hitchhiker. In effetti, la Guida galattica per gli autostoppisti è anche un libro su un libro chiamato Guida galattica per gli autostoppisti che contiene una miriade di consigli pratici per chi vuole viaggiare nella Galassia a un prezzo ragionevole (meno di trenta dollari altairiani al giorno, per essere precisi). E questo libro mostra "NON PANIC" sulla copertina, scritto a caratteri grandi e amichevoli.

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

Uscita:

42

Analizzo la domanda

42 è la risposta di Deep Thought to the Ultimate Question of Life, the Universe, and Everything. Questo è esattamente ciò che fa il codice di James M.. Ma capirai il funzionamento interiore del Pensiero Profondo?

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

Uscita:

42

La domanda a cui vogliono farci credere

Ecco un pezzo di codice di James Moran che mostra qual era la domanda, almeno quella che i topi vogliono farci pensare che fosse:

#include "stdio.h"

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

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

  return 0;
}

Uscita:

42

42 righe di codice

Un programma realizzato da Aman Saxena, sono 42 righe per visualizzarne 42! Nota l'importanza del numero esatto di 42 righe per visualizzare la risposta.

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

Uscita:

42

Abuso della compatibilità dell'assembler

Per citare il tweet di Shafik Yaghmour, "questo è un comportamento malvagio e indefinito, ma poiché si tratta solo di divertimento:"

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

Il programma restituisce 42. Come funziona? Questi sono codici esadecimali corrispondenti alle istruzioni assembler di un programma che restituisce solo 42!

Codice artistico

Un codice piacevole alla vista proposto dall'utente Reddit /r/drjeats. Nota lo sforzo di inserire esattamente 42 "1" al suo interno:

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

Uscita:

42

In effetti, l'esempio nel post di sfida mostrava forse lettere più regolari, ma non faceva esattamente i numeri giusti di "1":

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

Codice più artistico

Sempre in uno stile artistico, guarda questa bellissima rappresentazione di 42 dell'utente Reddit /r/d-frey che stampa anche un 42!

#include <iostream>

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

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

    ++ fourtytwo <<
    std::endl;
}

Uscita:

42

Derivare 42 da zero

Un'interessante derivazione di 42 letteralmente da zero da parte dell'utente Reddit /r/JavaSuck:

#include <iostream>

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

Uscita:

42

Vedere i bambini

L'utente di Reddit /r/renrutal spiega che "questo è il pezzo di codice più bello che stampa 42 perché non perdi tempo a chiederti cosa sta succedendo e puoi andare prima a casa per vedere i tuoi bellissimi bambini e chiunque sia veramente importante per voi". Una lezione di stile di vita!

#include <iostream>

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

Uscita:

42

Scatto con gli occhi bendati

Un simpatico algoritmo umoristico dell'utente Reddit /r/neoform:

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

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

Output:non è ancora uscito, ma spero che alla fine esca un 42. Con il tempo e un pizzico di fortuna.

Incremento zero

Per capire questo, il suo autore Zoltán Vajda commenta che "l'idea era di creare 42 incrementando zero più volte, dove né l'incremento, né il numero di incrementi sono 42". Come nota a margine, l'autore aggiunge che "questo, ovviamente, non ha senso, ma l'intera sfida non ha senso, quindi dovrebbe andare bene". 🙂

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

Uscita:

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

Algoritmi di Ben

Ben Deane ha usato questo evento del Towel Day come un'opportunità per scrivere un sacco di algoritmi carini, che finiscono per stampare i 42 desiderati. Gli algoritmi sono davvero fantastici.

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

Uscita:

42

Bitset

Ecco un'elaborata manipolazione dei bitset, di 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;
}

Uscita:

42

Intrigante rappresentazione binaria

Ecco un astuto codice modello di xffox che sfrutta il fatto che 42 sembra essere 101010 in binario:

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

Uscita:

42

{fmt}

Ecco come stampare 42 con la popolare libreria fmt, presentata dallo stesso autore, Victor Zverovitch:

#include <fmt/core.h>

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

Uscita:

42

Fibonacci

Sören Schellhoff usa una sottile combinazione di numeri di Fibonacci e operazioni aritmetiche, per arrivare alla risposta prevista:

/*
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;
}

Uscita:

42

Attaccati

L'utente di Twitter @PreeJackie invia il seguente pezzo di codice a Cling, l'interprete C++ interattivo, per produrre 42 nella sua REPL:

std::cout<<"42"

Ritagliato 42

Ecco un elaborato pezzo di codice di Emanuele Bonin dall'Italia, che stampa un altrettanto elaborato 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;
            }
        }

    }
}

Uscita:

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

La risposta e la domanda

Questo programma di Kevin Flitch dovrebbe alla fine produrne 42, insieme alla domanda che ha portato alla Risposta:

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

...ma non ha ancora finito di funzionare sulla mia macchina.

Era così semplice

Miroslav Franc ha inviato un breve e dolce pezzo di codice che dice tutto:

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

Uscita:

42

La domanda?

Il codice di Bartosz Borowik mostra che dopotutto potrebbe essere una domanda piuttosto semplice:

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

Uscita:

42

Conteggio bit

In questo secondo invio di Bartosz, la risposta viene da alcuni bit conteggiati:

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

Uscita:

42

Coroutine

mattnewport ha realizzato la sua presentazione per soddisfare i seguenti vincoli: nessuna cifra nel codice sorgente, produrre 42 implicitamente da alcuni calcoli alquanto interessanti, dimostrare come coroutine/generatori possono essere utilizzati per eseguire codice di stile funzionale in C++ (sequenze infinite/lazy con uno stile funzionale superiore funzioni di ordine).:

#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();
}

Uscita:

42

Direttive del compilatore

John Melas utilizza le direttive del compilatore specifiche del compilatore per produrre un 42. Non nella console, ma nell'output del compilatore:

#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

Una presentazione piuttosto divertente di 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;
}

Uscita:

42

K.I.S.S.

Arthur Bowers dice:“Ho letto in molti, molti posti (essendo qui uno di questi sono abbastanza sicuro...!) dicendo che il codice più bello è facile da leggere e capire. […] Ecco il mio intervento! BACI!"

#include <iostream>


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

Uscita:

42!

TMP

Se ti piace la metaprogrammazione dei modelli, apprezzerai la presentazione di Himanshu Tiwari:

//
//  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;
}

Uscita:

42

Fibonacci e Haiku

Graeme Williams ci mostra un ciclo for simile ad Haiku che sfrutta la sequenza di Fibonacci per produrre la risposta:

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

Uscita:

42

E conoscevi questo fatto curioso sul C++ moderno?

Ecco una rivelazione scioccante:la risposta all'ultima domanda alla vita, all'universo e a tutto è anche la risposta a cosa aggiungono le moderne versioni C++ (C++11, C++14, C++17)! Cosa pensare di questo?

Ecco il pezzo di codice che utilizza la bellissima libreria range-v3 per accompagnare questa scoperta di 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';
}

Uscita:

42

Presto e grazie per tutti i contributi

Spero che questo tributo alla Guida galattica per autostoppisti di Douglas Adams vi sia piaciuto. Trovo fantastico quello che avete fatto.

Se è piaciuto anche a te, condividilo! Mostra al mondo che l'ultima questione della vita, dell'universo e di tutto, la domanda che porta al 42, può assumere molte forme. Mostra loro come la creatività dei programmatori non abbia limiti, come il codice bello possa prendere cento facce, anche solo per stamparne 42.

E se non l'hai ancora fatto, perché non provare a leggere la serie di Guide per gli autostoppisti?

Addio, grazie per tutti i pesci e per tutti gli invii, e buona giornata dell'asciugamano!