Skip to content

Instantly share code, notes, and snippets.

@ClickerMonkey
Last active August 29, 2015 13:57
Show Gist options
  • Save ClickerMonkey/9911519 to your computer and use it in GitHub Desktop.
Save ClickerMonkey/9911519 to your computer and use it in GitHub Desktop.
Drive Ya Nuts Game Solver
public class DriveYaNuts
{
public static void main(String[] args) {
new DriveYaNuts();
}
/**
* BOARD POSITIONS
*
* _-1-_
* 6_ | _2
* | _-0-_ |
* 5_ | _3
* -4-
*
*/
// PIECES
public static final int PIECES[][] = {
// 3
// 5 2
// 6 4
// 1
{3,2,4,1,6,5},
// 2
// 6 4
// 1 5
// 3
{2,4,5,3,1,6},
// 2
// 4 5
// 6 3
// 1
{2,5,3,1,6,4},
// 6
// 1 5
// 2 4
// 3
{6,5,4,3,2,1},
// 6
// 3 5
// 4 2
// 1
{6,5,2,1,4,3},
// 4
// 3 5
// 2 6
// 1
{4,5,6,1,2,3},
// 2
// 6 3
// 4 5
// 1
{2,3,5,1,4,6}
};
// possible boards = 7 * 1 * 6 * 6 * 5 * 6 * 4 * 6 * 3 * 6 * 2 * 6 * 1 * 6 = 235,146,240
// valid boards = 2
public static final int PLACES = 7;
public static final int ROTATIONS = 6;
public DriveYaNuts() {
// piece 1 in one of 7 places
for (int a = 0; a < PLACES; a++) {
// piece 1 in one of 7 places WITHOUT rotation (notice the 1)
for (int ar = 0; ar < 1; ar++) {
// piece 2 in one of 7 places
for (int b = 0; b < PLACES; b++) {
// piece 2 in one of 7 places with rotation
for (int br = 0; br < ROTATIONS; br++) {
// can't place the same piece more than once!
if (a == b) {
continue;
}
// check #1
if (isValid(a, ar, b, br)) {
// piece 3 in one of 7 places
for (int c = 0; c < PLACES; c++) {
// piece 3 in one of 7 places with rotation
for (int cr = 0; cr < ROTATIONS; cr++) {
// can't place the same piece more than once!
if (a == c || b == c) {
continue;
}
// check #2
if (isValid(a, ar, b, br, c, cr)) {
// piece 4 in one of 7 places
for (int d = 0; d < PLACES; d++) {
// piece 4 in one of 7 places with rotation
for (int dr = 0; dr < ROTATIONS; dr++) {
// can't place the same piece more than once!
if (a == d || b == d || c == d) {
continue;
}
// check #3
if (isValid(a, ar, b, br, c, cr, d, dr)) {
// piece 5 in one of 7 places
for (int e = 0; e < PLACES; e++) {
// piece 5 in one of 5 places with rotation
for (int er = 0; er < ROTATIONS; er++) {
// can't place the same piece more than once!
if (a == e || b == e || c == e || d == e) {
continue;
}
// check #4
if (isValid(a, ar, b, br, c, cr, d, dr, e, er)) {
// piece 6 in one of 7 places
for (int f = 0; f < PLACES; f++) {
// piece 6 in one of 5 places with rotation
for (int fr = 0; fr < ROTATIONS; fr++) {
// can't place the same piece more than once!
if (a == f || b == f || c == f || d == f || e == f) {
continue;
}
// check #5
if (isValid(a, ar, b, br, c, cr, d, dr, e, er, f, fr)) {
// piece 7 in one of 7 places
for (int g = 0; g < PLACES; g++) {
// piece 7 in one of 5 places with rotation
for (int gr = 0; gr < ROTATIONS; gr++) {
// can't place the same piece more than once!
if (a == g || b == g || c == g || d == g || e == g || f == g) {
continue;
}
// check #5
if (isValid(a, ar, b, br, c, cr, d, dr, e, er, f, fr, g, gr)) {
System.out.format("0:%d(%d), 1:%d(%d), 2:%d(%d), 3:%d(%d), 4:%d(%d), 5:%d(%d), 6:%d(%d)\n", a, ar, b, br, c, cr, d, dr, e, er, f, fr, g, gr);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
class Check {
int min, piece1, side1, piece2, side2;
Check(int min, int piece1, int side1, int piece2, int side2) {
this.min = min;
this.piece1 = piece1;
this.side1 = side1;
this.piece2 = piece2;
this.side2 = side2;
}
}
public Check[] checks = {
new Check(2, 0, 0, 1, 3),
new Check(3, 0, 1, 2, 4), new Check(3, 1, 2, 2, 5),
new Check(4, 0, 2, 3, 5), new Check(4, 2, 3, 3, 0),
new Check(5, 0, 3, 4, 0), new Check(5, 3, 4, 4, 1),
new Check(6, 0, 4, 5, 1), new Check(6, 4, 5, 5, 2),
new Check(7, 0, 5, 6, 2), new Check(7, 5, 0, 6, 3),
};
public boolean isValid(int ... input) {
// #2. 0.0 = 1.3
// #3. 0.1 = 2.4, 1.2 = 2.5
// #4. 0.2 = 3.5, 2.3 = 3.0
// #5. 0.3 = 4.0, 3.4 = 4.1
// #6. 0.4 = 5.1, 4.5 = 5.2
// #7. 0.5 = 6.2, 5.0 = 6.3
int pieceCount = input.length / 2;
for (Check c : checks) {
// skip checks we've done in previous calls
if (c.min < pieceCount) {
continue;
}
// don't check pieces we don't have as input
if (c.min > pieceCount) {
break;
}
int a = input[c.piece1 * 2];
int ar = (c.side1 + input[c.piece1 * 2 + 1]) % 6;
int as = PIECES[a][ar];
int b = input[c.piece2 * 2];
int br = (c.side2 + input[c.piece2 * 2 + 1]) % 6;
int bs = PIECES[b][br];
// the sides don't match!
if (as != bs) {
return false;
}
}
// all checks passed
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment