Skip to content

Commit

Permalink
Fixing SonarQube issues
Browse files Browse the repository at this point in the history
  • Loading branch information
zodac committed Dec 21, 2024
1 parent 8776bef commit fbb63c8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 35 deletions.
56 changes: 27 additions & 29 deletions 2024/src/main/java/me/zodac/advent/Day15.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,13 @@ private static Pair<Point, Grid<Character>> 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<Point, Grid<Character>> movePoint(final Point from, final Point to, final Grid<Character> grid) {
Expand Down Expand Up @@ -159,7 +149,9 @@ private static Pair<Point, Grid<Character>> moveSplitBoxes(final Point originalP

private static Grid<Character> movePointsAlongPath(final List<Point> pointsToMove, final Direction direction, final Grid<Character> grid) {
Grid<Character> 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));
Expand Down Expand Up @@ -218,20 +210,26 @@ private static Point findMatch(final Point boxHalf, final char currentValue, fin
private static Grid<Character> expandGridWidthwise(final Grid<Character> 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;
}
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions common-utils/src/main/java/me/zodac/advent/grid/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -506,16 +506,14 @@ public Grid<E> rotate(final RotationDirection rotationDirection) {
public Set<Point> borderPoints() {
final Set<Point> 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;
Expand Down

0 comments on commit fbb63c8

Please sign in to comment.