Skip to content

Commit b06cbb3

Browse files
committed
refactor: remove not needed map resets at day 6
1 parent a6e40c6 commit b06cbb3

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

src/main/java/com/adventofcode/flashk/day06/GuardGallivant.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ public class GuardGallivant {
1111

1212
private static final char OBSTACLE = '#';
1313
private static final char OBSTRUCTION = 'O';
14-
private static final char UNVISITED = '.';
15-
private static final char VISITED = 'X';
14+
private static final char EMPTY = '.';
1615

1716
private final char[][] map;
1817
private final int rows;
@@ -50,7 +49,7 @@ public long solveB() {
5049
simulateGuardMovement();
5150
List<Vector2> possibleObstructionPositions = guardStates.stream().map(GuardState::pos)
5251
.distinct().filter(pos -> !pos.equals(initialGuardState.pos())).toList();
53-
resetMap();
52+
resetMap(null);
5453

5554
// Search for loops
5655
long loopCount = 0;
@@ -59,7 +58,7 @@ public long solveB() {
5958
if(simulateGuardMovement()) {
6059
loopCount++;
6160
}
62-
resetMap();
61+
resetMap(obstructionPos);
6362
}
6463

6564
return loopCount;
@@ -70,7 +69,6 @@ private boolean simulateGuardMovement() {
7069
boolean isLoop = false;
7170
Vector2 guardPos = initialGuardState.pos();
7271
Vector2 guardDir = initialGuardState.dir();
73-
7472
Vector2 nextPos;
7573

7674
do {
@@ -83,7 +81,6 @@ private boolean simulateGuardMovement() {
8381
isLoop = true;
8482
} else {
8583
guardStates.add(guardState);
86-
map[nextPos.getY()][nextPos.getX()] = VISITED;
8784
}
8885
} else {
8986
guardDir.rotateLeft();
@@ -106,17 +103,14 @@ private boolean isOutOfBounds(int row, int col) {
106103
return row < 0 || row >= rows || col < 0 || col >= cols;
107104
}
108105

109-
private void resetMap() {
106+
private void resetMap(Vector2 obstaclePos) {
110107

111108
guardStates = new HashSet<>();
112109
guardStates.add(initialGuardState);
113110

114-
for(int row = 0; row < rows; row++) {
115-
for(int col = 0; col < cols; col++) {
116-
if(map[row][col] != OBSTACLE) {
117-
map[row][col] = UNVISITED;
118-
}
119-
}
111+
if(obstaclePos != null) {
112+
map[obstaclePos.getY()][obstaclePos.getX()] = EMPTY;
120113
}
114+
121115
}
122116
}

0 commit comments

Comments
 (0)