Skip to content

Instantly share code, notes, and snippets.

@adriweb
Last active August 29, 2015 14:21
Show Gist options
  • Save adriweb/aa98e0d49d5e53c18a51 to your computer and use it in GitHub Desktop.
Save adriweb/aa98e0d49d5e53c18a51 to your computer and use it in GitHub Desktop.
Bruteforce solutions finder for the vietnamese math problem (see http://gu.com/p/493zk/stw)
// (C) Adrien "Adriweb" Bertrand
// Originally posted at http://ti-pla.net/t16528
// 2015-05-20
#include <stdio.h>
#include <stdint.h>
uint32_t solutionsFound = 0;
inline void swap(uint8_t *x, uint8_t *y) { if (x != y) { *x ^= *y; *y ^= *x; *x ^= *y; } }
void checkPermuts(uint8_t* array, uint8_t i, uint8_t length)
{
if (length == i) {
uint8_t* p = array;
float result = p[0] + 13.0 * (float)p[1] / (float)p[2] + p[3] + 12.0 * (float)p[4] - p[5] - 11.0 + (float)p[6] * (float)p[7] / (float)p[8] - 10;
if (result == 66.0) {
printf("%d+13*%d/%d+%d+12*%d-%d-11+%d*%d/%d-10 = 66\n", p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8]);
solutionsFound++;
}
return;
}
for (uint8_t j = i; j<length; j++) {
swap(array+i, array+j);
checkPermuts(array, i+1, length);
swap(array+i, array+j);
}
}
int main(int argc, char* argv[])
{
uint8_t possibleInts[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
checkPermuts(possibleInts, 0, 9);
printf("%d solutions found\n", solutionsFound);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment