Skip to content

Instantly share code, notes, and snippets.

@tkihira
Created December 26, 2014 03:53
Show Gist options
  • Save tkihira/069cd50cbe4568a4a3d2 to your computer and use it in GitHub Desktop.
Save tkihira/069cd50cbe4568a4a3d2 to your computer and use it in GitHub Desktop.
Meta-programming for ten puzzle
#include <stdio.h>
template<int a_bunshi, int a_bunbo>
struct Calc1 {
enum { result = (a_bunbo != 0 && a_bunbo * 10 == a_bunshi) };
};
template<int a_bunshi, int a_bunbo, int b_bunshi, int b_bunbo>
struct Calc2 {
enum {
result = Calc1<a_bunshi * b_bunbo + b_bunshi * a_bunbo, a_bunbo * b_bunbo>::result
|| Calc1<a_bunshi * b_bunbo - b_bunshi * a_bunbo, a_bunbo * b_bunbo>::result
|| Calc1<a_bunshi * b_bunshi, a_bunbo * b_bunbo>::result
|| Calc1<a_bunshi * b_bunbo, b_bunshi * a_bunbo>::result
};
};
template<int a_bunshi, int a_bunbo, int b_bunshi, int b_bunbo, int c_bunshi, int c_bunbo>
struct Calc3 {
enum {
result = Calc2<a_bunshi * b_bunbo + b_bunshi * a_bunbo, a_bunbo * b_bunbo, c_bunshi, c_bunbo>::result
|| Calc2<a_bunshi * b_bunbo - b_bunshi * a_bunbo, a_bunbo * b_bunbo, c_bunshi, c_bunbo>::result
|| Calc2<a_bunshi * b_bunshi, a_bunbo * b_bunbo, c_bunshi, c_bunbo>::result
|| Calc2<a_bunshi * b_bunbo, b_bunshi * a_bunbo, c_bunshi, c_bunbo>::result
|| Calc2<a_bunshi, a_bunbo, b_bunshi * c_bunbo + c_bunshi * b_bunbo, b_bunbo * c_bunbo>::result
|| Calc2<a_bunshi, a_bunbo, b_bunshi * c_bunbo - c_bunshi * b_bunbo, b_bunbo * c_bunbo>::result
|| Calc2<a_bunshi, a_bunbo, b_bunshi * c_bunshi, b_bunbo * c_bunbo>::result
|| Calc2<a_bunshi, a_bunbo, b_bunshi * c_bunbo, c_bunshi * b_bunbo>::result
};
};
template<int a_bunshi, int a_bunbo, int b_bunshi, int b_bunbo, int c_bunshi, int c_bunbo, int d_bunshi, int d_bunbo>
struct Calc4 {
enum {
result = Calc3<a_bunshi * b_bunbo + b_bunshi * a_bunbo, a_bunbo * b_bunbo, c_bunshi, c_bunbo, d_bunshi, d_bunbo>::result
|| Calc3<a_bunshi * b_bunbo - b_bunshi * a_bunbo, a_bunbo * b_bunbo, c_bunshi, c_bunbo, d_bunshi, d_bunbo>::result
|| Calc3<a_bunshi * b_bunshi, a_bunbo * b_bunbo, c_bunshi, c_bunbo, d_bunshi, d_bunbo>::result
|| Calc3<a_bunshi * b_bunbo, b_bunshi * a_bunbo, c_bunshi, c_bunbo, d_bunshi, d_bunbo>::result
|| Calc3<a_bunshi, a_bunbo, b_bunshi * c_bunbo + c_bunshi * b_bunbo, b_bunbo * c_bunbo, d_bunshi, d_bunbo>::result
|| Calc3<a_bunshi, a_bunbo, b_bunshi * c_bunbo - c_bunshi * b_bunbo, b_bunbo * c_bunbo, d_bunshi, d_bunbo>::result
|| Calc3<a_bunshi, a_bunbo, b_bunshi * c_bunshi, b_bunbo * c_bunbo, d_bunshi, d_bunbo>::result
|| Calc3<a_bunshi, a_bunbo, b_bunshi * c_bunbo, c_bunshi * b_bunbo, d_bunshi, d_bunbo>::result
|| Calc3<a_bunshi, a_bunbo, b_bunshi * c_bunbo + c_bunshi * b_bunbo, b_bunbo * c_bunbo, d_bunshi, d_bunbo>::result
|| Calc3<a_bunshi, a_bunbo, b_bunshi * c_bunbo - c_bunshi * b_bunbo, b_bunbo * c_bunbo, d_bunshi, d_bunbo>::result
|| Calc3<a_bunshi, a_bunbo, b_bunshi * c_bunshi, b_bunbo * c_bunbo, d_bunshi, d_bunbo>::result
|| Calc3<a_bunshi, a_bunbo, b_bunshi * c_bunbo, c_bunshi * b_bunbo, d_bunshi, d_bunbo>::result
|| Calc3<a_bunshi, a_bunbo, b_bunshi, b_bunbo, c_bunshi * d_bunbo + d_bunshi * c_bunbo, c_bunbo * d_bunbo>::result
|| Calc3<a_bunshi, a_bunbo, b_bunshi, b_bunbo, c_bunshi * d_bunbo - d_bunshi * c_bunbo, c_bunbo * d_bunbo>::result
|| Calc3<a_bunshi, a_bunbo, b_bunshi, b_bunbo, c_bunshi * d_bunshi, c_bunbo * d_bunbo>::result
|| Calc3<a_bunshi, a_bunbo, b_bunshi, b_bunbo, c_bunshi * d_bunbo, d_bunshi * c_bunbo>::result
};
};
template<int a, int b, int c, int d>
struct Calc {
enum { result = Calc4<a, 1, b, 1, c, 1, d, 1>::result };
};
template<int a, int b, int c, int d>
struct Test {
enum { result = Calc<a, b, c, d>::result
|| Calc<a, b, d, c>::result
|| Calc<a, b, d, c>::result
|| Calc<a, c, b, d>::result
|| Calc<a, c, b, d>::result
|| Calc<a, d, b, c>::result
|| Calc<a, d, c, b>::result
|| Calc<b, a, c, d>::result
|| Calc<b, a, d, c>::result
|| Calc<b, c, a, d>::result
|| Calc<b, c, d, a>::result
|| Calc<b, d, a, c>::result
|| Calc<b, d, c, a>::result
|| Calc<c, a, b, d>::result
|| Calc<c, a, d, b>::result
|| Calc<c, b, a, d>::result
|| Calc<c, b, d, a>::result
|| Calc<c, d, a, b>::result
|| Calc<c, d, b, a>::result
|| Calc<d, a, b, c>::result
|| Calc<d, a, c, b>::result
|| Calc<d, b, a, c>::result
|| Calc<d, b, c, a>::result
|| Calc<d, c, a, b>::result
|| Calc<d, c, b, a>::result };
};
int main() {
printf("%d\n", Test<1,2,3,4>::result);
printf("%d\n", Test<0,1,2,3>::result);
printf("%d\n", Test<1,1,5,8>::result);
printf("%d\n", Test<9,9,9,9>::result);
printf("%d\n", Test<5,5,5,7>::result);
printf("%d\n", Test<1,1,9,9>::result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment