D Paste by downs
Description: compile-time insanity
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | typedef bool red; typedef bool blue; typedef bool green; typedef bool white; struct cube(U, F, R, B, L, D) { alias U Up; alias F Front; alias R Right; alias B Back; alias L Left; alias D Down; } template tuple(T...) { alias T tuple; } alias cube!(blue, green, white, green, blue, red) cube1; alias cube!(white, green, blue, white, red, red) cube2; alias cube!(green, white, red, blue, red, red) cube3; alias cube!(blue, red, green, green, white, white) cube4; alias tuple!(cube1, cube2, cube3, cube4) cubes; template Rotation(C) { alias cube!(C.Up, C.Right, C.Back, C.Left, C.Front, C.Down) Rotation; } template Twist(C) { alias cube!(C.Front, C.Right, C.Up, C.Left, C.Down, C.Back) Twist; } template Flip(C) { alias cube!(C.Down, C.Left, C.Back, C.Right, C.Front, C.Up) Flip; } template Rotations(C) { alias tuple!(C, Rotation!(C), Rotation!(Rotation!(C)), Rotation!(Rotation!(Rotation!(C))) ) Rotations; } template Twists(C) { alias tuple!(C, Twist!(C), Twist!(Twist!(C))) Twists; } template Flips(C) { alias tuple!(C, Flip!(C)) Flips; } template map(alias T, U...) { static if (U.length) alias tuple!(T!(U[0]), map!(T, U[1..$])) map; else alias tuple!() map; } template _Orientations(C) { alias Rotations!(C) rots; alias map!(Twists, rots) twists; alias map!(Flips, twists) orients; } template Orientations(C) { alias _Orientations!(C).orients Orientations; } template Compatible(C1, C2) { const bool Compatible=!is(C1.Front==C2.Front) &&!is(C1.Back==C2.Back) &&!is(C1.Right==C2.Right) &&!is(C1.Left==C2.Left); } template Allowed(C, Pile...) { static if (!Pile.length) const bool Allowed=true; else const bool Allowed=Compatible!(C, Pile[$-1]) && Allowed!(C, Pile[0..$-1]); } // At this point I have ceased to understand the C++ program. // The rest is made-up. // :-) void iteratePiles(T...)() { static if (T.length==4) pragma(msg, "Valid solution: "~T.stringof); else { foreach (newface; Orientations!(cubes[T.length])) static if (Allowed!(newface, T)) iteratePiles!(T, newface)(); } } void main() { iteratePiles!()(); } |