LoginSignup
7
3

More than 5 years have passed since last update.

Re:強いエンジニアにHelloWorldさせてみた(縛りあり)(縛り追加)

Last updated at Posted at 2017-12-09

初心者C++Advent Calendar 2017

この記事は初心者C++Advent Calendar 2017 10日目の記事です

<< 9日目|他言語経験者はどのように C++ を学び始めるか || 11日目|autoの推論まとめ、あるいはautoは忖度などしない件 >>

本来もっとあとに記事を出す予定でしたがなんか空いているのとネタが手に入ったのでゆるふわ記事を出します。ガチな記事は12/15までお待ち下さい。

元ネタ

強いエンジニアにHelloWorldさせてみた(縛りあり)

すでにC++でやっている人もいて

「強いエンジニアにHelloWorldさせてみた」をC++でやってみました。

ですね。

既存のルール

  • ソースコードにダブルクォート、シングルクォート、数字を書くこと無く、「LIFULL」と出力して下さい。
  • 最後に改行を付けること。
  • 言語は問いません。

追加の縛り

  • C++11で、C++14ではない
  • 標準ライブラリのみ使用可、外部ライブラリ(boostとか)不可
  • マクロ使用禁止
  • sizeof演算子使用禁止
  • std::array使用禁止
  • enum/enum class使用禁止
  • コンパイル時に文字列を組み立てる
  • 文字コードがASCII互換(UTF-8とかCP932とか)、1byte=8bitな環境で動く

基本戦略

まあまず文字/文字列リテラルが書けず、数字も書けないのがもとの縛りです。この時点で普通のHelloWorldじゃない。

コンパイル時に文字列を組み立てる制約を追加したので実行時引数から文字列を渡すのはできません。

マクロは禁じたので先行例みたいに__LINE__マクロは使えません。またsizeof演算子も禁じたので配列の大きさから数字を作ることもできません。

幸いC++11縛りなので市民の義務であるconstexprが使えます。

というわけで愚直にASCIIの数値列を作ってやればいいことになります。

詳細

目的の文字列はASCIIでどういう数値列か

お題はLIFULLなので76, 73, 70, 85, 76, 76, 0となります。C/C++の文字列はNULL終端することをお忘れなく。

0と1をどう作るか

究極0と1さえ作れればあとはこねくり回してどうにかできます。

C++で0と1・・・といえばbool型ですね!C++はbool型から整数型に暗黙変換できる言語なので遠慮なく使いましょう!

constexpr char zero = false;
constexpr char one = true;

遠回りする手段としては

#include <type_traits>
constexpr char zero = std::true_type::value;
constexpr char one = std::false_type::value;

という手法やもっと遠回りしてC++14でなら

C++14
#include <limits>
constexpr char zero = std::numeric_limits<unsigned char>::min();
constexpr char one_impl()
{
    char t = zero;
    ++t;
    return t;
}
constexpr char one = one_impl();

という手もありますが、冗長なので素直にboolを使います。

大きい数をどうやって作るか

まあひたすら足し算や掛け算で作ってもいいんですが、流石にだるいのでstd::numeric_limitsの力を借りましょう。

例えば63という数字は1byte=8bitを仮定できるので

constexpr char two = one + one;
constexpr sixty_three = std::numeric_limits<unsigned char>::max() >> two;

と表せます。

別解としては
PythonでHelloWorld(縛り)
の記事でされているように、76=2*2*19=2*2*(3*3*2+1)を目指す方法もあります。

答え

あとは足し算やら引き算やら掛け算するだけですね。変数lifullがコンパイル時に組み上げられた文字列が格納されています。

#include <iostream>
#include <limits>
constexpr char zero = false;
constexpr char one = true;
constexpr char two = one + one;
constexpr char seventy = (std::numeric_limits<unsigned char>::max() >> two) + (two * two * two) - one;
constexpr char seventy_three = seventy + two + one;
constexpr char seventy_six = seventy_three + two + one;
constexpr char eighty_five = seventy_six + two * two * two + one;
constexpr const char lifull[] = { seventy_six, seventy_three, seventy, eighty_five, seventy_six, seventy_six, zero };
int main()
{
   std::cout << lifull << std::endl;
}

ありきたりな世界

個人的にHelloWorldと聞くとありきたりな世界なのでやってみましょう。

目指すのはarikitari_na_sekaiをデコードして97 114 105 107 105 116 97 114 105 95 110 97 95 115 101 107 97 105 0です。

100を超える数字がいくつも並んでいます。流石にヘルパー関数がほしいところです。

constexpr char zero = false;
constexpr char one = true;
constexpr char two = one + one;
constexpr char three = two + one;
constexpr char four = two + two;
constexpr char five = four + one;
constexpr char six = four + two;
constexpr char seven = six + one;
constexpr char eight = four * two;
constexpr char nine = three * three;
constexpr char ten = eight + two;
constexpr char fmt(char n) { return n; }
constexpr char fmt(char n1, char n2) { return n1 * ten + n2; }
constexpr char fmt(char n1, char n2, char n3) { return fmt(n1 * ten + n2, n3); }

というわけでこんなものを。char型なのでsigned/unsignedどちらにしろ4桁以上はないので可変長テンプレートを持ち出すまでもありません。

あとはよく出てくるaiは別に作っておくと可読性が上がりそうです。

constexpr char a = fmt(nine, seven);
constexpr char i = fmt(one, zero, five);

というわけで完成品

#include <iostream>
constexpr char zero = false;
constexpr char one = true;
constexpr char two = one + one;
constexpr char three = two + one;
constexpr char four = two + two;
constexpr char five = four + one;
constexpr char six = four + two;
constexpr char seven = six + one;
constexpr char eight = four * two;
constexpr char nine = three * three;
constexpr char ten = eight + two;
constexpr char fmt(char n) { return n; }
constexpr char fmt(char n1, char n2) { return n1 * ten + n2; }
constexpr char fmt(char n1, char n2, char n3) { return fmt(n1 * ten + n2, n3); }

constexpr char a = fmt(nine, seven);
constexpr char i = fmt(one, zero, five);
constexpr const char arikitari_na_sekai[] = {
  a, fmt(one, one, four), i, fmt(one, zero, seven), i, fmt(one, one, six), a, fmt(one, one, four), i,
  fmt(nine, five),
  fmt(one, one, zero), a,
  fmt(nine, five),
  fmt(one, one, five), fmt(one, zero, one), fmt(one, zero, seven), a, i,
  zero
};
int main()
{
  std::cout << arikitari_na_sekai << std::endl;
}

License

CC BY 4.0

CC-BY icon.svg

7
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
3