Last active
August 29, 2015 14:18
-
-
Save slowriot/58f1cb82b0d3babd2981 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <array> | |
#include <vector> | |
#include <iostream> | |
auto main()->int { | |
// use a sparse array to hold sums | |
std::array<std::vector<std::array<unsigned int, 3>>, 9 * 9 * 9> sums; | |
size_t maxsize = 0; | |
// first populate the sparse array | |
std::array<unsigned int, 3> triplet; | |
for(triplet[0] = 0; triplet[0] != 10; ++triplet[0]) { | |
for(triplet[1] = 0; triplet[1] != 10; ++triplet[1]) { | |
for(triplet[2] = 0; triplet[2] != 10; ++triplet[2]) { | |
unsigned int const sum = triplet[0] + triplet[1] + triplet[2]; | |
// add this triplet to the cache for this sum | |
sums[sum].emplace_back(triplet); | |
maxsize = std::max(maxsize, sums[sum].size()); | |
} | |
} | |
} | |
std::cout << "Max triplets with the same sum: " << maxsize << std::endl; | |
// then iterate through the sums and list all triplet combinations that match | |
unsigned int counter = 0; | |
for(auto const &sum : sums) { | |
for(auto const &left : sum) { | |
for(auto const &right : sum) { | |
std::cout << left[0] << left[1] << left[2] << right[0] << right[1] << right[2] << std::endl; | |
++counter; | |
} | |
} | |
} | |
std::cout << "Total: " << counter << std::endl; | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment