Skip to content

Instantly share code, notes, and snippets.

@Xirema
Last active March 27, 2019 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xirema/ece5f5751a44c3b528f5089ea842c888 to your computer and use it in GitHub Desktop.
Save Xirema/ece5f5751a44c3b528f5089ea842c888 to your computer and use it in GitHub Desktop.
#include<map>
#include<vector>
#include<array>
#include<iostream>
#include<iomanip>
#include<algorithm>
using arr_t = std::array<int, 6>;
std::map<arr_t, int64_t> get_rolls() {
std::map<arr_t, int64_t> map;
for(int i = 1; i <= 10; i++) {
arr_t arr = {i, 11, 11, 11, 11, 11};
map[arr]++;
}
for(int i = 1; i < 6; i++) {
std::map<arr_t, int64_t> temp;
for(auto const& [arr, trials] : map) {
for(int value = 1; value <= 10; value++) {
auto arr_copy = arr;
arr_copy[i] = value;
std::sort(arr_copy.begin(), arr_copy.end());
temp[arr_copy] += trials;
}
}
map = temp;
}
return map;
}
int get_largest_even(arr_t const& arr) {
int ret = -1;
for(int i : arr) {
if(i % 2 == 0) {
if(auto it = std::find(arr.begin(), arr.end(), i / 2); it != arr.end())
ret = i;
}
}
return ret;
}
auto filter_rolls(std::map<arr_t, int64_t> const& map) {
std::map<int, int64_t> new_map;
for(auto const& [arr, trials] : map) {
int largest_even = get_largest_even(arr);
if(largest_even != -1) {
new_map[largest_even] += trials;
}
}
return new_map;
}
int main() {
auto rolls = get_rolls();
auto filtered_rolls = filter_rolls(rolls);
int64_t total_trials = 0;
for(auto const& [largest_even, trials] : filtered_rolls) {
std::cout <<
"Sum of " << std::setw(2) << largest_even + (largest_even / 2) <<
" " << std::setw(6) << trials << " trials (" <<
std::setw(6) << std::setprecision(3) << std::fixed << (trials / 10'000.0) << "%)\n";
total_trials += trials;
}
std::cout << "===\n";
std::cout << "Total: " << std::setw(6) << total_trials <<
" trials " << std::setw(6) << (total_trials / 10'000.0) << "%\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment