Cheating at (some) maths puzzles

like a lazy computer scientist

Owen Cliffe, 2015 @zootalures

Soma Cube

  • given pieces consisting of
    • all possible combinations of 3-4 cubes
    • joined at their faces
    • s.t. at least one corner is formed
    • excluding rotational symmetries
  • then these fit into a 3x3 cube
    • in 240 distinct ways (excluding symmetries)

The pieces


% 3 axes
axis(x). axis(y). axis(z).

% 3 positions
pos(0). pos(1). pos(2).

% index the location of each part in each axis
loc(x,p(X,Y,Z),X) ↤ element(p(X,Y,Z)).
loc(y,p(X,Y,Z),Y) ↤ element(p(X,Y,Z)).
loc(z,p(X,Y,Z),Z) ↤ element(p(X,Y,Z)).
             

% generate all combinations of 3 and four blocks in the cube
3 { element(p(X,Y,Z)) : pos(X), pos(Y), pos(Z)} 4.
             

20475 solutions ( 27 choose 3 + 27 choose 4)

Connectedness


% Two pieces are adjacent if they share the same coordinate in
% two axes and are one apart in the other
adjacent(A,P1,P2) ↤
loc(A,P1,R), loc(A, P2, R+1), % adjacent axes
loc(B,P1,S), loc(B, P2, S), % same axes
loc(C,P1,T), loc(C, P2, T), % same axes
B!=C,A!=B,A!=C,B!=C, element(P1),element(P2).

% connected is adjacent
connected(P1,P2) ↤ adjacent (_,P1, P2).

% connectedness is transitive
connected(P1,P3) ↤ connected(P1,P2), connected(P2,P3) .

% model is disconnected if any two parts are not conected
disconnected ↤ not connected(P1, P2), element(P1), element(P2).

% exclude solutions which are disconnected
⊥↤ disconnected.
             

98 solutions

Ignore rotational symmetries


 % possible rotations to build octahedral symmetry
 rotv(x, 0). rotv(x, 90). rotv(x, 180). rotv(x, 270).
 rotv(y, 0). rotv(y, 90). rotv(y, 180).
 rotv(z, 0). rotv(z, 90).

 % trig functions we need
 sin(0,0). sin(90,1). sin(180,0). sin(270,-1).
 cos(0,1). cos(90,0). cos(180,-1). cos(270,0).

 % Pick one rotation per axis from possible list
 1{ rot(P,A,R) : rotv(A,R) } 1 :- piece(P),axis(A).
              

rotated_part(r(RX,RY,RZ),p(IX,IY,IZ),p(RX_X,RX_Y,RX_Z)) :-
    rotv(x,RX),rotv(y,RY),  rotv(z,RZ),
    pos(IX), pos(IY), pos(IZ),
    sin(RX,SRX), sin(RY,SRY), sin(RZ,SRZ),
    cos(RX,CRX), cos(RY,CRY), cos(RZ,CRZ),
    RX_X = ((IZ * SRY) + (((IX * CRZ) - (IY * SRZ)) * CRY)),
    RX_Y = ((((IX * SRZ) + (IY * CRZ)) * CRX) - (((IZ  * CRY) - (((IX * CRZ) - (IY * SRZ)) * SRY)) * SRX)),
    RX_Z = ((((IX * SRZ) + (IY * CRZ)) * SRX) + (((IZ  * CRY) - (((IX * CRZ) - (IY * SRZ)) * SRY)) * CRX)).
              

24 rotations

9 pieces

                
% a corner consists of three adjacent pieces, adjacent in two distinct axes.
corner(P2,A3) ↤
adjacent(A1,P1,P2), adjacent(A2,P2,P3),
axis(A3), A1!=A2, A2!=A3,
P1!=P3 , A1!=A2,A1!=A3.

% a piece has inflections if it has at least one corner
at_least_one_corner ↤ corner(P2,A3).

% Exclude models without any corners
⊥ ↤ not at_least_one_corner.
                

8 pieces

                
% corners are opposite if they share two adjacent pieces
oppositecorners(P1,P2,A) ↤ corner(P1,A), corner(P2,A),
adjacent(_,P1,P3), adjacent(_,P2,P3), P1!=P3,P1!=P2.

% opposite corners are not infjections
at_least_one_corner :- corner(P2,A3), not oppositecorners(P1,_,A3),element(P1).
⊥ ↤ not at_least_one_corner.

                

7 pieces

Solving the cube


% pick exactly one rotation and translation for each piece
1{transform_element(P,R,T) : poss_location(P,R,T,_) }1 ↤ piece(P) .


% place each element of each piece according to its translation and rotation
element(P,L)↤ transform_element(P,R,T),poss_location(P,R,T,L).


% two elements cannot occupy the same coordinate
⊥ ↤ element(P1,L),element(P2,L),P1!=P2.


% each element in the cube must be occupied
occupied(L) ↤ element(P,L).
⊥ ↤ not occupied(L), loc(L).

11520 solutions

Links