-
Notifications
You must be signed in to change notification settings - Fork 0
/
rubiks.pl
251 lines (241 loc) · 10.7 KB
/
rubiks.pl
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
% %
% Rubik's Cube Solver
% Solve a 3x3x3 Rubik's Cube
%
% Authors -
% Trent Rand <contact@Trentrand.com>
% Robert Dinaro
%
% Documentation -
% Please see the README file or presentation for more information.
%
% Usage -
% With your cube in the correct orientation, as explained in the documentation, input your cube map to generate a solution.
% Input each face in the order specfied above by the 'Order of Input' representation above.
%
% For example, the following is how you can solve a solved cube with F,R,U applied:
% | :- solve(Solution, cube(g,w,w,g,w,w,r,r,r,b,b,o,y,y,o,y,y,o,g,g,r,g,g,r,y,y,b,o,b,b,o,b,b,g,b,b,w,w,w,r,r,y,r,r,y,o,o,w,o,o,w,y,g,g), C), solved(C).
%
% %
% Define the base case, where all colors are in correct position and the cube is solved
solved(cube(W,W,W,W,W,W,W,W,W,Y,Y,Y,Y,Y,Y,Y,Y,Y,G,G,G,G,G,G,G,G,G,B,B,B,B,B,B,B,B,B,R,R,R,R,R,R,R,R,R,O,O,O,O,O,O,O,O,O)).
% Solve the Rubik's Cube by inputting your cube map as variable Cube
solve([], Cube, Cube).
solve([NextRotation | Rotation], Cube, NewState) :- solve(Rotation, CurrentState, NewState), rotateside(NextRotation, Cube, CurrentState).
% Side Rotation translation maps-
% Rotate the Up side clockwise by 90 degrees
% From: To:
%
% O1 O2 O3 O1 O2 O3
% O4 O5 O6 O4 O5 O6
% O7 O8 O9 G9 G6 G3
% G1 G2 G3 W1 W2 W3 B1 B2 B3 G1 G2 R1 W7 W4 W1 O7 B2 B3
% G4 G5 G6 W4 W5 W6 B4 B5 B6 G4 G5 R2 W8 W5 W2 O8 B5 B6
% G7 G8 G9 W7 W8 W9 B7 B8 B9 G7 G8 R3 W9 W6 W3 O9 B8 B9
% R1 R2 R3 B7 B4 B1
% R4 R5 R6 R4 R5 R6
% R7 R8 R9 R7 R8 R9
% Y1 Y2 Y3 Y1 Y2 Y3
% Y4 Y5 Y6 Y4 Y5 Y6
% Y7 Y8 Y9 Y7 Y8 Y9
rotateside(
up,
% translate cube map from:
cube(
W1, W2, W3, W4, W5, W6, W7, W8, W9, % top side
Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8, Y9, % bottom side
G1, G2, G3, G4, G5, G6, G7, G8, G9, % left side
B1, B2, B3, B4, B5, B6, B7, B8, B9, % right side
R1, R2, R3, R4, R5, R6, R7, R8, R9, % front side
O1, O2, O3, O4, O5, O6, O7, O8, O9 % back side
),
% to new cube map with Up side rotated clockwise by 90 degrees:
cube(
W7, W4, W1, W8, W5, W2, W9, W6, W3, % top side
Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8, Y9, % bottom side
G1, G2, R1, G4, G5, R2, G7, G8, R3, % left side
O7, B2, B3, O8, B5, B6, O9, B8, B9, % right side
B7, B4, B1, R4, R5, R6, R7, R8, R9, % front side
O1, O2, O3, O4, O5, O6, G9, G6, G3 % back side
)
).
% Rotate the Down side clockwise by 90 degrees
% From: To:
%
% O1 O2 O3 G7 G4 G1
% O4 O5 O6 O4 O5 O6
% O7 O8 O9 O7 O8 O9
% G1 G2 G3 W1 W2 W3 B1 B2 B3 R7 G2 G3 W1 W2 W3 B1 B2 O1
% G4 G5 G6 W4 W5 W6 B4 B5 B6 R8 G5 G6 W4 W5 W6 B4 B5 O2
% G7 G8 G9 W7 W8 W9 B7 B8 B9 R9 G8 G9 W7 W8 W9 B7 B8 O3
% R1 R2 R3 R1 R2 R3
% R4 R5 R6 R4 R5 R6
% R7 R8 R9 B9 B6 B3
% Y1 Y2 Y3 Y3 Y6 Y9
% Y4 Y5 Y6 Y2 Y5 Y8
% Y7 Y8 Y9 Y1 Y4 Y7
rotateside(
down,
% translate cube map from:
cube(
W1, W2, W3, W4, W5, W6, W7, W8, W9, % top side
Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8, Y9, % bottom side
G1, G2, G3, G4, G5, G6, G7, G8, G9, % left side
B1, B2, B3, B4, B5, B6, B7, B8, B9, % right side
R1, R2, R3, R4, R5, R6, R7, R8, R9, % front side
O1, O2, O3, O4, O5, O6, O7, O8, O9 % back side
),
% to new cube map with Down side rotated clockwise by 90 degrees:
cube(
W1, W2, W3, W4, W5, W6, W7, W8, W9, % top side
Y3, Y6, Y9, Y2, Y5, Y8, Y1, Y4, Y7, % bottom side
R7, G2, G3, R8, G5, G6, R9, G8, G9, % left side
B1, B2, O1, B4, B5, O2, B7, B8, O3, % right side
R1, R2, R3, R4, R5, R6, B9, B6, B3, % front side
G7, G4, G1, O4, O5, O6, O7, O8, O9 % back side
)
).
% Rotate the Left side clockwise by 90 degrees
% From: To:
%
% O1 O2 O3 Y1 O2 O3
% O4 O5 O6 Y4 O5 O6
% O7 O8 O9 Y7 O8 O9
% G1 G2 G3 W1 W2 W3 B1 B2 B3 G7 G4 G1 O1 W2 W3 B1 B2 B3
% G4 G5 G6 W4 W5 W6 B4 B5 B6 G8 G5 G2 O4 W5 W6 B4 B5 B6
% G7 G8 G9 W7 W8 W9 B7 B8 B9 G9 G6 G3 O7 W8 W9 B7 B8 B9
% R1 R2 R3 W1 R2 R3
% R4 R5 R6 W4 R5 R6
% R7 R8 R9 W7 R8 R9
% Y1 Y2 Y3 R1 Y2 Y3
% Y4 Y5 Y6 R4 Y5 Y6
% Y7 Y8 Y9 R7 Y8 Y9
rotateside(
left,
% translate cube map from:
cube(
W1, W2, W3, W4, W5, W6, W7, W8, W9, % top side
Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8, Y9, % bottom side
G7, G4, G1, G8, G5, G2, G9, G6, G3, % left side
B1, B2, B3, B4, B5, B6, B7, B8, B9, % right side
R1, R2, R3, R4, R5, R6, R7, R8, R9, % front side
O1, O2, O3, O4, O5, O6, O7, O8, O9 % back side
),
% to new cube map with Left side rotated clockwise by 90 degrees:
cube(
O1, W2, W3, O4, W5, W6, O7, W8, W9, % top side
R1, Y2, Y3, R4, Y5, Y6, R7, Y8, Y9, % bottom side
G7, G4, G1, G8, G5, G2, G9, G6, G3, % left side
B1, B2, B3, B4, B5, B6, B7, B8, B9, % right side
W1, R2, R3, W4, R5, R6, W7, R8, R9, % front side
Y1, O2, O3, Y4, O5, O6, Y7, O8, O9 % back side
)
).
% Rotate the Right side clockwise by 90 degrees
% From: To:
%
% O1 O2 O3 O1 O2 W3
% O4 O5 O6 O4 O5 W6
% O7 O8 O9 O7 O8 W9
% G1 G2 G3 W1 W2 W3 B1 B2 B3 G1 G2 G3 W1 W2 R3 B7 B4 B1
% G4 G5 G6 W4 W5 W6 B4 B5 B6 G4 G5 G6 W4 W5 R6 B8 B5 B2
% G7 G8 G9 W7 W8 W9 B7 B8 B9 G7 G8 G9 W7 W8 R9 B9 B6 B3
% R1 R2 R3 R1 R2 Y3
% R4 R5 R6 R4 R5 Y6
% R7 R8 R9 R7 R8 Y9
% Y1 Y2 Y3 Y1 Y2 O3
% Y4 Y5 Y6 Y4 Y5 O6
% Y7 Y8 Y9 Y7 Y8 O9
rotateside(
right,
% translate cube map from:
cube(
W1, W2, W3, W4, W5, W6, W7, W8, W9, % top side
Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8, Y9, % bottom side
G1, G2, G3, G4, G5, G6, G7, G8, G9, % left side
B1, B2, B3, B4, B5, B6, B7, B8, B9, % right side
R1, R2, R3, R4, R5, R6, R7, R8, R9, % front side
O1, O2, O3, O4, O5, O6, O7, O8, O9 % back side
),
% to new cube map with Right side rotated clockwise by 90 degrees:
cube(
W1, W2, R3, W4, W5, R6, W7, W8, R9, % top side
Y1, Y2, O3, Y4, Y5, O6, Y7, Y8, O9, % bottom side
G1, G2, G3, G4, G5, G6, G7, G8, G9, % left side
B7, B4, B1, B8, B5, B2, B9, B6, B3, % right side
R1, R2, Y3, R4, R5, Y6, R7, R8, Y9, % front side
O1, O2, W3, O4, O5, W6, O7, O8, W9 % back side
)
).
% Rotate the Front side clockwise by 90 degrees
% From: To:
%
% O1 O2 O3 O1 O2 O3
% O4 O5 O6 O4 O5 O6
% O7 O8 O9 O7 O8 O9
% G1 G2 G3 W1 W2 W3 B1 B2 B3 G1 G2 G3 W1 W2 W3 B1 B2 B3
% G4 G5 G6 W4 W5 W6 B4 B5 B6 G4 G5 G6 W4 W5 W6 B4 B5 B6
% G7 G8 G9 W7 W8 W9 B7 B8 B9 Y3 Y2 Y1 G7 G8 G9 W7 W8 W9
% R1 R2 R3 R7 R4 R1
% R4 R5 R6 R8 R5 R2
% R7 R8 R9 R9 R6 R3
% Y1 Y2 Y3 B9 B8 B7
% Y4 Y5 Y6 Y4 Y5 Y6
% Y7 Y8 Y9 Y7 Y8 Y9
rotateside(
front,
% translate cube map from:
cube(
W1, W2, W3, W4, W5, W6, W7, W8, W9, % top side
Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8, Y9, % bottom side
G1, G2, G3, G4, G5, G6, G7, G8, G9, % left side
B1, B2, B3, B4, B5, B6, B7, B8, B9, % right side
R1, R2, R3, R4, R5, R6, R7, R8, R9, % front side
O1, O2, O3, O4, O5, O6, O7, O8, O9 % back side
),
% to new cube map with Front side rotated clockwise by 90 degrees:
cube(
W1, W2, W3, W4, W5, W6, G7, G8, G9, % top side
B9, B8, B7, Y4, Y5, Y6, Y7, Y8, Y9, % bottom side
G1, G2, G3, G4, G5, G6, Y3, Y2, Y1, % left side
B1, B2, B3, B4, B5, B6, W7, W8, W9, % right side
R7, R4, R1, R8, R5, R2, R9, R6, R3, % front side
O1, O2, O3, O4, O5, O6, O7, O8, O9 % back side
)
).
% Rotate the Back side clockwise by 90 degrees
% From: To:
%
% O1 O2 O3 O3 O6 O9
% O4 O5 O6 O2 O5 O8
% O7 O8 O9 O1 O4 O7
% G1 G2 G3 W1 W2 W3 B1 B2 B3 Y9 Y8 Y7 G1 G2 G3 W1 W2 W3
% G4 G5 G6 W4 W5 W6 B4 B5 B6 G4 G5 G6 W4 W5 W6 B4 B5 B6
% G7 G8 G9 W7 W8 W9 B7 B8 B9 G7 G8 G9 W7 W8 W9 B7 B8 B9
% R1 R2 R3 R1 R2 R3
% R4 R5 R6 R4 R5 R6
% R7 R8 R9 R7 R8 R9
% Y1 Y2 Y3 Y1 Y2 Y3
% Y4 Y5 Y6 Y4 Y5 Y6
% Y7 Y8 Y9 B3 B2 B1
rotateside(
back,
% translate cube map from:
cube(
W1, W2, W3, W4, W5, W6, W7, W8, W9, % top side
Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8, Y9, % bottom side
G1, G2, G3, G4, G5, G6, G7, G8, G9, % left side
B1, B2, B3, B4, B5, B6, B7, B8, B9, % right side
R1, R2, R3, R4, R5, R6, R7, R8, R9, % front side
O1, O2, O3, O4, O5, O6, O7, O8, O9 % back side
),
% to new cube map with Back side rotated clockwise by 90 degrees:
cube(
G1, G2, G3, W4, W5, W6, W7, W8, W9, % top side
Y1, Y2, Y3, Y4, Y5, Y6, B3, B2, B1, % bottom side
Y9, Y8, Y7, G4, G5, G6, G7, G8, G9, % left side
W1, W2, W3, B4, B5, B6, B7, B8, B9, % right side
R1, R2, R3, R4, R5, R6, R7, R8, R9, % front side
O3, O6, O9, O2, O5, O8, O1, O4, O7 % back side
)
).