Advertisement
dark-s0ul

solve

Apr 14th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. public class HexagonCell {
  2.     boolean state;
  3.     Set<Integer> nbs;
  4.  
  5.     HexagonCell(boolean state, Set<Integer> nbs) {
  6.         this.state = state;
  7.         this.nbs = nbs;
  8.     }
  9. }
  10.  
  11. public int get_index(int x, int y) {
  12.     int index = 0;
  13.     for (Hexagon hex1 : hexagons) {
  14.         if (hex1.coordX == x && hex1.coordY == y) {
  15.             return index;
  16.         }
  17.         index++;
  18.     }
  19.     return -1;
  20. }
  21.  
  22. public HexagonCell[] prepare_cells() {
  23.     HexagonCell[] out = new HexagonCell[hexagons.size()];
  24.  
  25.     int[][] nbs1 = nbs;
  26.  
  27.     boolean isOdd = type.equals("odd");
  28.  
  29.     int index = 0;
  30.     for (Hexagon hex1 : hexagons) {
  31.         Set<Integer> eq = new HashSet<>();
  32.         eq.add(index);
  33.  
  34.         int x = hex1.coordX;
  35.         int y = hex1.coordY;
  36.  
  37.         if (isOdd) {
  38.             nbs1 = y % 2 == 0 ? nbsOdd : nbsEven;
  39.         }
  40.  
  41.         for (int[] nb : nbs1) {
  42.             int idx = get_index(x + nb[0], y + nb[1]);
  43.  
  44.             if (idx != -1) {
  45.                 eq.add(idx);
  46.             }
  47.         }
  48.         out[index++] = new HexagonCell(hex1.state, eq);
  49.     }
  50.  
  51.     return out;
  52. }
  53.  
  54. public Set<Integer> unique_set(Set<Integer> a, Set<Integer> b) {
  55.     Set<Integer> out = new HashSet<>();
  56.  
  57.     for (int i : a) {
  58.         if (!b.contains(i)) {
  59.             out.add(i);
  60.         }
  61.     }
  62.  
  63.     for (int i : b) {
  64.         if (!a.contains(i)) {
  65.             out.add(i);
  66.         }
  67.     }
  68.  
  69.     return out;
  70. }
  71.  
  72. public HexagonCell[] solve() {
  73.     HexagonCell[] cells = prepare_cells();
  74.  
  75.     int len = cells.length;
  76.  
  77.     for (int i = 0; i < len; i++) {
  78.         for (int j = i; j < len; j++) {
  79.             if (cells[j].nbs.contains(i)) {
  80.                 HexagonCell t = cells[i];
  81.                 cells[i] = cells[j];
  82.                 cells[j] = t;
  83.                 break;
  84.             }
  85.         }
  86.  
  87.         for (int j = i + 1; j < len; j++) {
  88.             if (cells[j].nbs.contains(i)) {
  89.                 cells[j].state ^= cells[i].state;
  90.                 cells[j].nbs = unique_set(cells[j].nbs, cells[i].nbs);
  91.             }
  92.         }
  93.     }
  94.  
  95.     for (int i = len - 1; i >= 0; i--) {
  96.         for (int j = 0; j < i; j++) {
  97.             if (cells[j].nbs.contains(i)) {
  98.                 cells[j].state ^= cells[i].state;
  99.                 cells[j].nbs = unique_set(cells[j].nbs, cells[i].nbs);
  100.             }
  101.         }
  102.     }
  103.     return cells;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement