From fbb63c81a7a7e2049d51ce9be7c731226dc93a11 Mon Sep 17 00:00:00 2001 From: zodac Date: Sat, 21 Dec 2024 17:13:56 +1300 Subject: [PATCH] Fixing SonarQube issues --- 2024/src/main/java/me/zodac/advent/Day15.java | 56 +++++++++---------- .../main/java/me/zodac/advent/grid/Grid.java | 10 ++-- 2 files changed, 31 insertions(+), 35 deletions(-) diff --git a/2024/src/main/java/me/zodac/advent/Day15.java b/2024/src/main/java/me/zodac/advent/Day15.java index 37d47b6..f11061d 100644 --- a/2024/src/main/java/me/zodac/advent/Day15.java +++ b/2024/src/main/java/me/zodac/advent/Day15.java @@ -93,23 +93,13 @@ private static Pair> movePointAndBoxes(final Point point, final Point nextPoint = point.move(direction); final Character nextValue = grid.at(nextPoint); - if (nextValue == EMPTY_SYMBOL) { - return movePoint(point, nextPoint, grid); - } - - if (nextValue == OBSTACLE_SYMBOL) { - return Pair.of(point, grid); - } - - if (nextValue == BOX_SYMBOL) { - return moveBoxes(point, direction, grid, nextPoint); - } - - if (BOX_HALVES.contains(nextValue)) { - return moveSplitBoxes(point, direction, grid, nextPoint, nextValue); - } - - throw new IllegalStateException("Unrecognized symbol at point: " + nextPoint); + return switch (nextValue) { + case EMPTY_SYMBOL -> movePoint(point, nextPoint, grid); + case OBSTACLE_SYMBOL -> Pair.of(point, grid); + case BOX_SYMBOL -> moveBoxes(point, direction, grid, nextPoint); + case SPLIT_BOX_LEFT_SYMBOL, SPLIT_BOX_RIGHT_SYMBOL -> moveSplitBoxes(point, direction, grid, nextPoint, nextValue); + default -> throw new IllegalStateException("Unrecognized symbol at point: " + nextPoint); + }; } private static Pair> movePoint(final Point from, final Point to, final Grid grid) { @@ -159,7 +149,9 @@ private static Pair> moveSplitBoxes(final Point originalP private static Grid movePointsAlongPath(final List pointsToMove, final Direction direction, final Grid grid) { Grid updatedGrid = grid; - for (int i = pointsToMove.size() - 1; i >= 0; i--) { + final int numberOfPoints = pointsToMove.size() - 1; + + for (int i = numberOfPoints; i >= 0; i--) { final Point from = pointsToMove.get(i); final Point to = from.move(direction); updatedGrid = updatedGrid.updateAt(to, grid.at(from)); @@ -218,20 +210,26 @@ private static Point findMatch(final Point boxHalf, final char currentValue, fin private static Grid expandGridWidthwise(final Grid grid) { final int newGridSize = grid.size() << 1; final Character[][] newInternalGrid = new Character[grid.size()][newGridSize]; + final int numberOfRows = grid.numberOfRows(); + final int numberOfColumns = grid.numberOfColumns(); - for (int i = 0; i < grid.numberOfRows(); i++) { - for (int j = 0; j < grid.numberOfColumns(); j++) { + for (int i = 0; i < numberOfRows; i++) { + for (int j = 0; j < numberOfColumns; j++) { final char current = grid.at(i, j); - if (current == BOX_SYMBOL) { - newInternalGrid[i][(j << 1)] = SPLIT_BOX_LEFT_SYMBOL; - newInternalGrid[i][(j << 1) + 1] = SPLIT_BOX_RIGHT_SYMBOL; - } else if (current == START_SYMBOL) { - newInternalGrid[i][(j << 1)] = START_SYMBOL; - newInternalGrid[i][(j << 1) + 1] = EMPTY_SYMBOL; - } else { - newInternalGrid[i][(j << 1)] = current; - newInternalGrid[i][(j << 1) + 1] = current; + switch (current) { + case BOX_SYMBOL -> { + newInternalGrid[i][(j << 1)] = SPLIT_BOX_LEFT_SYMBOL; + newInternalGrid[i][(j << 1) + 1] = SPLIT_BOX_RIGHT_SYMBOL; + } + case START_SYMBOL -> { + newInternalGrid[i][(j << 1)] = START_SYMBOL; + newInternalGrid[i][(j << 1) + 1] = EMPTY_SYMBOL; + } + default -> { + newInternalGrid[i][(j << 1)] = current; + newInternalGrid[i][(j << 1) + 1] = current; + } } } } diff --git a/common-utils/src/main/java/me/zodac/advent/grid/Grid.java b/common-utils/src/main/java/me/zodac/advent/grid/Grid.java index 45a10e9..6d2a1d3 100644 --- a/common-utils/src/main/java/me/zodac/advent/grid/Grid.java +++ b/common-utils/src/main/java/me/zodac/advent/grid/Grid.java @@ -506,16 +506,14 @@ public Grid rotate(final RotationDirection rotationDirection) { public Set borderPoints() { final Set borderPoints = new HashSet<>(); - // Top and bottom borders - for (int col = 0; col < numberOfColumns(); col++) { + for (int col = 0; col < internalGrid[0].length; col++) { borderPoints.add(Point.of(0, col)); // Top row - borderPoints.add(Point.of(numberOfRows() - 1, col)); // Bottom row + borderPoints.add(Point.of(internalGrid.length - 1, col)); // Bottom row } - // Left and right borders - for (int row = 1; row < numberOfRows(); row++) { + for (int row = 1; row < internalGrid.length; row++) { borderPoints.add(Point.of(row, 0)); // Left column - borderPoints.add(Point.of(row, numberOfColumns() - 1)); // Right column + borderPoints.add(Point.of(row, internalGrid[0].length - 1)); // Right column } return borderPoints;