Created
October 2, 2015 11:49
-
-
Save alsanchez/313b4610184428cc7d81 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.lang.reflect.Array; | |
import java.security.MessageDigest; | |
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
List<Cell> todas = new ArrayList<Cell>(); | |
todas.add(new Cell(0, 0)); | |
todas.add(new Cell(1, 0)); | |
todas.add(new Cell(2, 0)); | |
todas.add(new Cell(0, 1)); | |
todas.add(new Cell(1, 1)); | |
todas.add(new Cell(2, 1)); | |
todas.add(new Cell(0, 2)); | |
todas.add(new Cell(1, 2)); | |
todas.add(new Cell(2, 2)); | |
int size = 8; | |
byte[] origin = new byte[size]; | |
for(;;) | |
{ | |
List<Cell> celdas = getNextCombination(todas, origin, size); | |
if(celdas == null) | |
{ | |
break; | |
} | |
String resultado = patternToString(celdas); | |
if (resultado.substring(0, 10).equals("6145494400")) | |
{ | |
for(int i=0;i<size;i++) | |
{ | |
System.out.println(celdas.get(i).getRow() + ", " + celdas.get(i).getColumn()); | |
} | |
} | |
} | |
} | |
private static List<Cell> getNextCombination(List<Cell> todas, byte[] origin, int size) | |
{ | |
origin[size-1]++; | |
for(int i=size-1;i>=0;i--) | |
{ | |
if(origin[i] > 8) | |
{ | |
if(i == 0) | |
{ | |
return null; | |
} | |
origin[i] = 0; | |
origin[i-1]++; | |
} | |
} | |
ArrayList<Cell> list = new ArrayList<Cell>(size); | |
for(int i=0;i<size;i++) | |
{ | |
list.add(todas.get(origin[i])); | |
} | |
return list; | |
} | |
public static byte[] patternToByteArray(final List<Cell> list) { | |
byte[] array; | |
if (list == null) { | |
array = null; | |
} | |
else { | |
final int size = list.size(); | |
final byte[] array2 = new byte[size]; | |
int n = 0; | |
while (true) { | |
array = array2; | |
if (n >= size) { | |
break; | |
} | |
final Cell cell = list.get(n); | |
array2[n] = (byte)(cell.getRow() * 3 + cell.getColumn()); | |
++n; | |
} | |
} | |
return array; | |
} | |
public static String byteArrayToHexString(final byte[] array) { | |
String string = ""; | |
for (int i = 0; i < array.length; ++i) { | |
string = String.valueOf(string) + Integer.toString((array[i] & 0xFF) + 256, 16).substring(1); | |
} | |
return string; | |
} | |
public static String patternToString(final List<Cell> list) { | |
final byte[] patternToByteArray = patternToByteArray(list); | |
if (patternToByteArray == null) { | |
return ""; | |
} | |
try { | |
return byteArrayToHexString(MessageDigest.getInstance("SHA-1").digest(patternToByteArray)); | |
} | |
catch (Exception ex) { | |
return ""; | |
} | |
} | |
private static class Cell | |
{ | |
private int row; | |
private int column; | |
public Cell(int row, int column) | |
{ | |
this.row = row; | |
this.column = column; | |
} | |
public int getRow() | |
{ | |
return row; | |
} | |
public int getColumn() | |
{ | |
return column; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment