Skip to content

Instantly share code, notes, and snippets.

@maksverver
Last active October 3, 2015 22:45
Show Gist options
  • Save maksverver/80a79e1b27fc74a9a8d8 to your computer and use it in GitHub Desktop.
Save maksverver/80a79e1b27fc74a9a8d8 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
/* Tiles are labeled clockwise starting from the top.
Colors are r(ed), y(ellow), g(reen), b(lue).
Uppercase for head, lowercase for butt. */
char tiles[9][4] = {
"yrGB",
"RbyG",
"BrbG",
"RgyB",
"YgrB",
"brBY",
"YryG",
"RgyG",
"rbBG" };
void print() {
for (int r = 0; r < 3; ++r) {
for (int c = 0; c < 3; ++c) printf(" %c ", tiles[3*r + c][0]);
printf("\n");
for (int c = 0; c < 3; ++c) printf("%c %c", tiles[3*r + c][3], tiles[3*r + c][1]);
printf("\n");
for (int c = 0; c < 3; ++c) printf(" %c ", tiles[3*r + c][2]);
printf("\n");
}
}
int match(char x, char y) {
return (x^y) == ('A'^'a');
}
void rotate(int i) {
char tmp = tiles[i][0];
tiles[i][0] = tiles[i][1];
tiles[i][1] = tiles[i][2];
tiles[i][2] = tiles[i][3];
tiles[i][3] = tmp;
}
void swap(int i, int j) {
for (int n = 0; n < 4; ++n) {
char tmp = tiles[i][n];
tiles[i][n] = tiles[j][n];
tiles[j][n] = tmp;
}
}
void search(int i) {
if (i == 9) {
printf("Solution found:\n");
print();
return;
}
int r = i/3, c = i%3;
for (int j = i; j < 9; ++j) {
swap(i, j);
for (int n = 0; n < 4; ++n) {
if ((r == 0 || match(tiles[i][0], tiles[i - 3][2])) &&
(c == 0 || match(tiles[i][3], tiles[i - 1][1])))
search(i + 1);
rotate(i);
}
swap(i, j);
}
}
int main() {
search(0);
}
/* Output:
Solution found:
G G B
r Bb Bb G
y r r
Y R R
B bB gG g
r y y
R Y Y
G bB gG r
y r y
Solution found:
G B r
y Rr Yy G
b b B
B B b
r Yy Rr G
g g B
G G b
y Yy Rr B
r g G
Solution found:
B G y
r Yy Rr G
g g Y
G G y
r Bb Bb G
y r R
Y R r
B bB gG b
r y B
Solution found:
B r r
r Yy Gg B
b B Y
B b y
y Rr Gg G
g B R
G b r
B rR yY y
b G G
Solution found:
y r y
r Gg Bb G
Y Y R
y y r
g Gg Bb B
R R Y
r r y
G bB bB r
B G G
Solution found:
G G b
y Yy Rr B
r b G
R B g
G gG rR y
y b B
Y B b
B gG yY r
r r B
Solution found:
B y r
b Gg Bb B
r R Y
R r y
G bB bB r
y G G
Y g g
G rR yY r
y G B
Solution found:
G g r
B rR yY y
b G G
B g g
G rR yY r
b B B
B b b
G yY rR y
r B G
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment