Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package afdw.jt;
- import com.google.common.collect.ImmutableList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.stream.Collectors;
- import java.util.stream.LongStream;
- public class Main2 {
- private static final List<Shape> SHAPES = ImmutableList.<Shape>builder()
- .add(
- new Shape(
- "A",
- 1,
- 4,
- new boolean[][] {
- {true},
- {true},
- {true},
- {true}
- }
- )
- )
- .add(
- new Shape(
- "B",
- 4,
- 1,
- new boolean[][] {
- {true, true, true, true}
- }
- )
- )
- .add(
- new Shape(
- "C",
- 3,
- 2,
- new boolean[][] {
- {true, true, true},
- {false, false, true}
- }
- )
- )
- .add(
- new Shape(
- "D",
- 3,
- 2,
- new boolean[][] {
- {false, true, true},
- {true, true, false}
- }
- )
- )
- .add(
- new Shape(
- "E",
- 2,
- 2,
- new boolean[][] {
- {true, true},
- {true, true}
- }
- )
- )
- .add(
- new Shape(
- "F",
- 3,
- 2,
- new boolean[][] {
- {true, true, false},
- {false, true, true}
- }
- )
- )
- .add(
- new Shape(
- "G",
- 2,
- 3,
- new boolean[][] {
- {true, true},
- {false, true},
- {false, true}
- }
- )
- )
- .add(
- new Shape(
- "H",
- 3,
- 2,
- new boolean[][] {
- {true, true, true},
- {false, true, false}
- }
- )
- )
- .build();
- private static final int BOARD_SIZE = 6;
- @SuppressWarnings("SuspiciousNameCombination")
- public static void main(String[] args) {
- LongStream shapesPosesStream = LongStream.of(0);
- AtomicInteger maxFail = new AtomicInteger(0);
- int i = 0;
- for (Shape shape : SHAPES) {
- int finalI = i;
- shapesPosesStream = shapesPosesStream
- .flatMap(shapesPoses ->
- LongStream.rangeClosed(0, BOARD_SIZE - shape.height)
- .flatMap(y ->
- LongStream.rangeClosed(0, BOARD_SIZE - shape.width)
- .map(x -> x << 4 | y)
- )
- .map(pos -> shapesPoses << 8 | pos)
- )
- .filter(shapesPoses -> {
- String[][] board = new String[BOARD_SIZE][BOARD_SIZE];
- for (int j = 0; j <= finalI; j++) {
- long pos = (shapesPoses >> ((finalI - j) * 8)) & 0b11111111;
- if (!SHAPES.get(j).fillBoard(board, (int) ((pos >> 4) & 0b1111), (int) (pos & 0b1111))) {
- if (j > maxFail.get()) {
- maxFail.set(j);
- }
- return false;
- }
- }
- return true;
- });
- i++;
- }
- shapesPosesStream = shapesPosesStream.parallel();
- List<String[][]> boards = shapesPosesStream
- .mapToObj(shapesPoses -> {
- String[][] board = new String[BOARD_SIZE][BOARD_SIZE];
- for (int j = 0; j < SHAPES.size(); j++) {
- long pos = (shapesPoses >> ((SHAPES.size() - 1 - j) * 8)) & 0b11111111;
- if (!SHAPES.get(j).fillBoard(board, (int) ((pos >> 4) & 0b1111), (int) (pos & 0b1111))) {
- throw new IllegalStateException();
- }
- }
- return board;
- })
- .collect(Collectors.toList());
- if (boards.isEmpty()) {
- System.out.println("Failed to place " + maxFail.get() + " shape");
- }
- System.out.println(
- boards.stream()
- .map(board ->
- Arrays.stream(board)
- .map(ss ->
- Arrays.stream(ss)
- .map(s -> s == null ? "." : s)
- .collect(Collectors.joining(""))
- )
- .collect(Collectors.joining("\n"))
- )
- .collect(Collectors.joining("\n---\n"))
- );
- }
- private static final class Shape {
- @SuppressWarnings("WeakerAccess")
- public final String name;
- @SuppressWarnings("WeakerAccess")
- public final int width;
- @SuppressWarnings("WeakerAccess")
- public final int height;
- @SuppressWarnings("WeakerAccess")
- public final boolean[][] data;
- @SuppressWarnings("WeakerAccess")
- public Shape(String name, int width, int height, boolean[][] data) {
- this.name = name;
- this.width = width;
- this.height = height;
- this.data = data;
- }
- @SuppressWarnings("WeakerAccess")
- public boolean fillBoard(String[][] board, int startX, int startY) {
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- if (data[y][x]) {
- if (board[startY + y][startX + x] != null) {
- return false;
- }
- board[startY + y][startX + x] = name;
- }
- }
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement