libdo

Untitled

Nov 2nd, 2017
6,211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package afdw.jt;
  2.  
  3. import com.google.common.collect.ImmutableList;
  4.  
  5. import java.util.Arrays;
  6. import java.util.List;
  7. import java.util.concurrent.atomic.AtomicInteger;
  8. import java.util.stream.Collectors;
  9. import java.util.stream.LongStream;
  10.  
  11. public class Main2 {
  12.     private static final List<Shape> SHAPES = ImmutableList.<Shape>builder()
  13.         .add(
  14.             new Shape(
  15.                 "A",
  16.                 1,
  17.                 4,
  18.                 new boolean[][] {
  19.                     {true},
  20.                     {true},
  21.                     {true},
  22.                     {true}
  23.                 }
  24.             )
  25.         )
  26.         .add(
  27.             new Shape(
  28.                 "B",
  29.                 4,
  30.                 1,
  31.                 new boolean[][] {
  32.                     {true, true, true, true}
  33.                 }
  34.             )
  35.         )
  36.         .add(
  37.             new Shape(
  38.                 "C",
  39.                 3,
  40.                 2,
  41.                 new boolean[][] {
  42.                     {true, true, true},
  43.                     {false, false, true}
  44.                 }
  45.             )
  46.         )
  47.         .add(
  48.             new Shape(
  49.                 "D",
  50.                 3,
  51.                 2,
  52.                 new boolean[][] {
  53.                     {false, true, true},
  54.                     {true, true, false}
  55.                 }
  56.             )
  57.         )
  58.         .add(
  59.             new Shape(
  60.                 "E",
  61.                 2,
  62.                 2,
  63.                 new boolean[][] {
  64.                     {true, true},
  65.                     {true, true}
  66.                 }
  67.             )
  68.         )
  69.         .add(
  70.             new Shape(
  71.                 "F",
  72.                 3,
  73.                 2,
  74.                 new boolean[][] {
  75.                     {true, true, false},
  76.                     {false, true, true}
  77.                 }
  78.             )
  79.         )
  80.         .add(
  81.             new Shape(
  82.                 "G",
  83.                 2,
  84.                 3,
  85.                 new boolean[][] {
  86.                     {true, true},
  87.                     {false, true},
  88.                     {false, true}
  89.                 }
  90.             )
  91.         )
  92.         .add(
  93.             new Shape(
  94.                 "H",
  95.                 3,
  96.                 2,
  97.                 new boolean[][] {
  98.                     {true, true, true},
  99.                     {false, true, false}
  100.                 }
  101.             )
  102.         )
  103.         .build();
  104.     private static final int BOARD_SIZE = 6;
  105.  
  106.     @SuppressWarnings("SuspiciousNameCombination")
  107.     public static void main(String[] args) {
  108.         LongStream shapesPosesStream = LongStream.of(0);
  109.         AtomicInteger maxFail = new AtomicInteger(0);
  110.         int i = 0;
  111.         for (Shape shape : SHAPES) {
  112.             int finalI = i;
  113.             shapesPosesStream = shapesPosesStream
  114.                 .flatMap(shapesPoses ->
  115.                     LongStream.rangeClosed(0, BOARD_SIZE - shape.height)
  116.                         .flatMap(y ->
  117.                             LongStream.rangeClosed(0, BOARD_SIZE - shape.width)
  118.                                 .map(x -> x << 4 | y)
  119.                         )
  120.                         .map(pos -> shapesPoses << 8 | pos)
  121.                 )
  122.                 .filter(shapesPoses -> {
  123.                     String[][] board = new String[BOARD_SIZE][BOARD_SIZE];
  124.                     for (int j = 0; j <= finalI; j++) {
  125.                         long pos = (shapesPoses >> ((finalI - j) * 8)) & 0b11111111;
  126.                         if (!SHAPES.get(j).fillBoard(board, (int) ((pos >> 4) & 0b1111), (int) (pos & 0b1111))) {
  127.                             if (j > maxFail.get()) {
  128.                                 maxFail.set(j);
  129.                             }
  130.                             return false;
  131.                         }
  132.                     }
  133.                     return true;
  134.                 });
  135.             i++;
  136.         }
  137.         shapesPosesStream = shapesPosesStream.parallel();
  138.         List<String[][]> boards = shapesPosesStream
  139.             .mapToObj(shapesPoses -> {
  140.                 String[][] board = new String[BOARD_SIZE][BOARD_SIZE];
  141.                 for (int j = 0; j < SHAPES.size(); j++) {
  142.                     long pos = (shapesPoses >> ((SHAPES.size() - 1 - j) * 8)) & 0b11111111;
  143.                     if (!SHAPES.get(j).fillBoard(board, (int) ((pos >> 4) & 0b1111), (int) (pos & 0b1111))) {
  144.                         throw new IllegalStateException();
  145.                     }
  146.                 }
  147.                 return board;
  148.             })
  149.             .collect(Collectors.toList());
  150.         if (boards.isEmpty()) {
  151.             System.out.println("Failed to place " + maxFail.get() + " shape");
  152.         }
  153.         System.out.println(
  154.             boards.stream()
  155.                 .map(board ->
  156.                     Arrays.stream(board)
  157.                         .map(ss ->
  158.                             Arrays.stream(ss)
  159.                                 .map(s -> s == null ? "." : s)
  160.                                 .collect(Collectors.joining(""))
  161.                         )
  162.                         .collect(Collectors.joining("\n"))
  163.                 )
  164.                 .collect(Collectors.joining("\n---\n"))
  165.         );
  166.     }
  167.  
  168.     private static final class Shape {
  169.         @SuppressWarnings("WeakerAccess")
  170.         public final String name;
  171.         @SuppressWarnings("WeakerAccess")
  172.         public final int width;
  173.         @SuppressWarnings("WeakerAccess")
  174.         public final int height;
  175.         @SuppressWarnings("WeakerAccess")
  176.         public final boolean[][] data;
  177.  
  178.         @SuppressWarnings("WeakerAccess")
  179.         public Shape(String name, int width, int height, boolean[][] data) {
  180.             this.name = name;
  181.             this.width = width;
  182.             this.height = height;
  183.             this.data = data;
  184.         }
  185.  
  186.         @SuppressWarnings("WeakerAccess")
  187.         public boolean fillBoard(String[][] board, int startX, int startY) {
  188.             for (int y = 0; y < height; y++) {
  189.                 for (int x = 0; x < width; x++) {
  190.                     if (data[y][x]) {
  191.                         if (board[startY + y][startX + x] != null) {
  192.                             return false;
  193.                         }
  194.                         board[startY + y][startX + x] = name;
  195.                     }
  196.                 }
  197.             }
  198.             return true;
  199.         }
  200.     }
  201. }
Add Comment
Please, Sign In to add comment