Skip to content

Instantly share code, notes, and snippets.

@KayEss
Created May 22, 2015 07:13
Show Gist options
  • Save KayEss/413a1f1a532ba1c92b02 to your computer and use it in GitHub Desktop.
Save KayEss/413a1f1a532ba1c92b02 to your computer and use it in GitHub Desktop.
Brute force solution for Vietnamese snake problem
// Compile as C++14
// clang++ --std=c++14 snake.cpp -O3 -o /tmp/snake && /tmp/snake
// http://www.theguardian.com/science/alexs-adventures-in-numberland/2015/may/20/can-you-do-the-maths-puzzle-for-vietnamese-eight-year-olds-that-has-stumped-parents-and-teachers
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::size_t tries = 0, wins = 0;
std::vector<double> digits = {1,2,3,4,5,6,7,8,9};
do {
++tries;
double result = digits[0] + 13.0 * digits[1] / digits[2] + digits[3]
+ 12.0 * digits[4] - digits[5] - 11.0 + digits[6] * digits[7] / digits[8] - 10.0;
if ( result == 66.0 ) {
++wins;
std::cout << digits[0] << " + 13 * " << digits[1] << " / " << digits[2]
<< " + " << digits[3] << " + 12 * " << digits[4] << " - " << digits[5]
<< " - 11 + " << digits[6] << " * " << digits[7] << " / " << digits[8]
<< " - 10 = " << result << std::endl;
}
} while ( std::next_permutation(digits.begin(), digits.end()) );
std::cout << tries << " tries and " << wins << " solutions" << std::endl;
}
/* Ouput:
1 + 13 * 2 / 6 + 4 + 12 * 7 - 8 - 11 + 3 * 5 / 9 - 10 = 66
1 + 13 * 2 / 6 + 4 + 12 * 7 - 8 - 11 + 5 * 3 / 9 - 10 = 66
1 + 13 * 3 / 2 + 4 + 12 * 5 - 8 - 11 + 7 * 9 / 6 - 10 = 66
1 + 13 * 3 / 2 + 4 + 12 * 5 - 8 - 11 + 9 * 7 / 6 - 10 = 66
1 + 13 * 3 / 2 + 9 + 12 * 5 - 6 - 11 + 4 * 7 / 8 - 10 = 66
1 + 13 * 3 / 2 + 9 + 12 * 5 - 6 - 11 + 7 * 4 / 8 - 10 = 66
1 + 13 * 3 / 4 + 7 + 12 * 6 - 5 - 11 + 2 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- 7 - 11 + 1 * 3 / 2 - 10 = 66
9 + 13 * 4 / 8 + 5 + 12 * 6 - 7 - 11 + 3 * 1 / 2 - 10 = 66
9 + 13 * 5 / 3 + 1 + 12 * 4 - 2 - 11 + 7 * 8 / 6 - 10 = 66
9 + 13 * 5 / 3 + 1 + 12 * 4 - 2 - 11 + 8 * 7 / 6 - 10 = 66
9 + 13 * 6 / 4 + 3 + 12 * 5 - 8 - 11 + 1 * 7 / 2 - 10 = 66
9 + 13 * 6 / 4 + 3 + 12 * 5 - 8 - 11 + 7 * 1 / 2 - 10 = 66
9 + 13 * 8 / 6 + 2 + 12 * 4 - 1 - 11 + 5 * 7 / 3 - 10 = 66
9 + 13 * 8 / 6 + 2 + 12 * 4 - 1 - 11 + 7 * 5 / 3 - 10 = 66
362880 tries and 128 solutions
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment