|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +public class PiecingItTogether { |
| 4 | + private static final double DOUBLE_EQUALITY_TOLERANCE = 1e-9; |
| 5 | + private static final int MAX_DIMENSION = 1000; |
| 6 | + |
| 7 | + public static JigsawInfo getCompleteInformation(JigsawInfo input) { |
| 8 | + List<JigsawInfo> validGuesses = new ArrayList<>(); |
| 9 | + |
| 10 | + if (input.getPieces().isPresent()) { |
| 11 | + // If pieces is known, we only test divisors of pieces |
| 12 | + int pieces = input.getPieces().getAsInt(); |
| 13 | + for (int rows = 1; rows <= pieces; rows++) { |
| 14 | + if (pieces % rows != 0) { |
| 15 | + continue; |
| 16 | + } |
| 17 | + int columns = pieces / rows; |
| 18 | + createValidJigsaw(rows, columns, input).ifPresent(validGuesses::add); |
| 19 | + } |
| 20 | + } else if (input.getInside().isPresent() && input.getInside().getAsInt() > 0) { |
| 21 | + // If inside pieces is non-zero, we only test divisors of inside |
| 22 | + int inside = input.getInside().getAsInt(); |
| 23 | + for (int innerRows = 1; innerRows <= inside; innerRows++) { |
| 24 | + if (inside % innerRows != 0) { |
| 25 | + continue; |
| 26 | + } |
| 27 | + int innerColumns = inside / innerRows; |
| 28 | + createValidJigsaw(innerRows + 2, innerColumns + 2, input).ifPresent(validGuesses::add); |
| 29 | + } |
| 30 | + } else { |
| 31 | + // Brute force using border constraint if available |
| 32 | + int maxDimension = input.getBorder().isPresent() |
| 33 | + ? Math.min(input.getBorder().getAsInt(), MAX_DIMENSION) |
| 34 | + : MAX_DIMENSION; |
| 35 | + |
| 36 | + for (int rows = 1; rows <= maxDimension; rows++) { |
| 37 | + for (int columns = 1; columns <= maxDimension; columns++) { |
| 38 | + createValidJigsaw(rows, columns, input).ifPresent(validGuesses::add); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + if (validGuesses.size() == 1) { |
| 44 | + return validGuesses.get(0); |
| 45 | + } else if (validGuesses.size() > 1) { |
| 46 | + throw new IllegalArgumentException("Insufficient data"); |
| 47 | + } else { |
| 48 | + throw new IllegalArgumentException("Contradictory data"); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private static String getFormatFromAspect(double aspectRatio) { |
| 53 | + if (Math.abs(aspectRatio - 1.0) < DOUBLE_EQUALITY_TOLERANCE) { |
| 54 | + return "square"; |
| 55 | + } else if (aspectRatio < 1.0) { |
| 56 | + return "portrait"; |
| 57 | + } else { |
| 58 | + return "landscape"; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private static JigsawInfo fromRowsAndCols(int rows, int columns) { |
| 63 | + int pieces = rows * columns; |
| 64 | + int border = (rows == 1 || columns == 1) ? pieces : 2 * (rows + columns - 2); |
| 65 | + int inside = pieces - border; |
| 66 | + double aspectRatio = (double) columns / rows; |
| 67 | + String format = getFormatFromAspect(aspectRatio); |
| 68 | + |
| 69 | + return new JigsawInfo.Builder() |
| 70 | + .pieces(pieces) |
| 71 | + .border(border) |
| 72 | + .inside(inside) |
| 73 | + .rows(rows) |
| 74 | + .columns(columns) |
| 75 | + .aspectRatio(aspectRatio) |
| 76 | + .format(format) |
| 77 | + .build(); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Verifies that all known values in the input match those in the computed result. |
| 82 | + * Returns false if any known value conflicts, true if all values are consistent. |
| 83 | + * |
| 84 | + * @param computed the fully inferred jigsaw information |
| 85 | + * @param input the original partial input with possibly empty values |
| 86 | + * @return true if all values are consistent, false if any conflict exists |
| 87 | + */ |
| 88 | + private static boolean isConsistent(JigsawInfo computed, JigsawInfo input) { |
| 89 | + return valuesMatch(computed.getPieces(), input.getPieces()) && |
| 90 | + valuesMatch(computed.getBorder(), input.getBorder()) && |
| 91 | + valuesMatch(computed.getInside(), input.getInside()) && |
| 92 | + valuesMatch(computed.getRows(), input.getRows()) && |
| 93 | + valuesMatch(computed.getColumns(), input.getColumns()) && |
| 94 | + valuesMatch(computed.getAspectRatio(), input.getAspectRatio()) && |
| 95 | + valuesMatch(computed.getFormat(), input.getFormat()); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Attempts to construct a valid jigsaw configuration using the specified number of rows and columns. |
| 100 | + * Returns a valid result only if the configuration is consistent with the input. |
| 101 | + * |
| 102 | + * @param rows number of rows to try |
| 103 | + * @param columns number of columns to try |
| 104 | + * @param input the original input to check for consistency |
| 105 | + * @return an Optional containing a valid configuration, or empty if inconsistent |
| 106 | + */ |
| 107 | + private static Optional<JigsawInfo> createValidJigsaw(int rows, int columns, JigsawInfo input) { |
| 108 | + JigsawInfo candidate = fromRowsAndCols(rows, columns); |
| 109 | + return isConsistent(candidate, input) ? Optional.of(candidate) : Optional.empty(); |
| 110 | + } |
| 111 | + |
| 112 | + private static <T> boolean valuesMatch(Optional<T> a, Optional<T> b) { |
| 113 | + if (a.isPresent() && b.isPresent()) { |
| 114 | + return Objects.equals(a.get(), b.get()); |
| 115 | + } |
| 116 | + |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + private static boolean valuesMatch(OptionalInt a, OptionalInt b) { |
| 121 | + if (a.isPresent() && b.isPresent()) { |
| 122 | + return a.getAsInt() == b.getAsInt(); |
| 123 | + } |
| 124 | + |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + private static boolean valuesMatch(OptionalDouble a, OptionalDouble b) { |
| 129 | + if (a.isPresent() && b.isPresent()) { |
| 130 | + return Double.compare(a.getAsDouble(), b.getAsDouble()) == 0; |
| 131 | + } |
| 132 | + |
| 133 | + return true; |
| 134 | + } |
| 135 | +} |
0 commit comments