Skip to content

Instantly share code, notes, and snippets.

@akosma
Last active March 24, 2017 11:07
Show Gist options
  • Save akosma/58dbb90e1390a00f76c6964a76b155fd to your computer and use it in GitHub Desktop.
Save akosma/58dbb90e1390a00f76c6964a76b155fd to your computer and use it in GitHub Desktop.
Files used for the free book contest

Book Draw

This is the code used to decide the winner of the draw for a free copy of my book "Android for iOS Developers" on March 24th, 2017, as announced on Twitter.

Requirements

This C++ program has been tested on macOS Sierra 10.12.3 with the latest Xcode developer tools, Homebrew and CMake. You can install CMake using Homebrew:

brew install cmake

Build and Run

Copy the files CMakeLists.txt and main.cpp into a folder, cd into that folder and type the following commands:

cmake .
make
./bookdraw

Winner

The draw is available in asciinema, and the winner is @yostane!

cmake_minimum_required(VERSION 3.6)
project(bookdraw)
set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES main.cpp)
add_executable(bookdraw ${SOURCE_FILES})
#include <iostream>
#include <random>
#include <sstream>
using namespace std;
using Players = vector<string>;
int main() {
Players players{"@0xced", "@GuickPa", "@HerveCaumont", "@LakaLe_", "@MarcioK",
"@Neilkachu", "@VijayTholpadi", "@a_schacherbauer", "@agisilaosts",
"@apouche", "@azouts", "@dmytrolisitsyn", "@draufunddran1", "@ge_hn",
"@guiiipontes", "@igorescobar", "@ikarius", "@jadekin",
"@janiskirsteins", "@jolyAlexandre", "@kedarv", "@kyegeorge",
"@leakywellington", "@lipec", "@m_sgr_bot", "@marius_const",
"@onmyway133", "@ooswald", "@paulrikmans", "@pbendersky",
"@relausen", "@rpresedo", "@rshankra", "@sandeepCool77", "@sconig",
"@whakkee", "@xmonodev", "@xrb", "@yostane", "@zen_prog"};
auto shuffle = [](Players &p) -> void {
auto seed = chrono::system_clock::now().time_since_epoch().count();
std::shuffle(p.begin(), p.end(), default_random_engine(seed));
};
auto stats = [](Players &p) -> string {
auto join = [p](string separator) -> string {
ostringstream stream;
auto iterator = ostream_iterator<string>(stream, separator.c_str());
copy(p.begin(), p.end(), iterator);
auto str = stream.str();
str.erase(str.length() - separator.size());
return str;
};
stringstream stream;
stream << p.size() << " players: " << join(", ");
return stream.str();
};
auto congrats = [](string &winner, uint32_t number) -> string {
string medal{"\U0001F3C5"};
string cup{"\U0001F3C6"};
string gift{"\U0001F381"};
string thumbsup{"\U0001F44D"};
stringstream stream;
stream << "And the winner is… " << medal << " " << cup
<< " ~~ " << winner << " ~~ "
<< gift << " " << thumbsup << " (number " << number << ")";
return stream.str();
};
auto draw = [shuffle](Players &p) {
auto times = arc4random_uniform(1000);
uint32_t lottery;
string winner;
for (int i = 0; i < times; ++i) {
shuffle(p);
lottery = arc4random_uniform(p.size());
winner = p[lottery];
}
return make_pair(lottery, winner);
};
auto result = draw(players);
auto lottery = result.first;
auto winner = result.second;
cout << stats(players) << endl;
cout << endl;
cout << congrats(winner, lottery) << endl;
cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment