Skip to content

Commit

Permalink
refactor: refactor day 20
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 20, 2024
1 parent 2ea9beb commit 982e1f2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 43 deletions.
40 changes: 21 additions & 19 deletions src/main/java/com/adventofcode/flashk/day20/RaceCondition.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,12 @@ public RaceCondition(char[][] inputs) {
}
}

// Apply dijkstra to obtain distance to any tile from the start
// This allows to obtain distance from start node (S) to any node (X): d(S,X)
// and also allows to calculate distance from start node (S) to end node (E): d(S,E)
dijkstra(start);
}

// Now add to every node (X) the relative distance from that node to the end node (E):
// d(X,E) = d(S,E) - d(S,X)
for(int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
RaceTile tile = map[row][col];
if(!tile.isWall()) {
tile.setDistanceToEnd(end.getDistance() - tile.getDistance());
}
}
public void runRaces(int cheatTime) {

if(cheatTime < 2) {
throw new IllegalArgumentException("Cheat time must be at least 2");
}

// The idea of the algorithm for both part 1 and 2 is that total distance when cheating on any node will be:
Expand All @@ -102,16 +94,26 @@ public RaceCondition(char[][] inputs) {
// d(B, E) = 0
// So the total distance will from S to E will be X.
// Keep in mind that distance will be lower than the normal d(S, E) distance.
}

public void runRaces(int cheatTime) {

if(cheatTime < 2) {
throw new IllegalArgumentException("Cheat time must be at least 2");
// Apply dijkstra to obtain distance to any tile from the start
// This allows to obtain distance from start node (S) to any node (X): d(S,X)
// and also allows to calculate distance from start node (S) to end node (E): d(S,E)
dijkstra(start);

// Now add to every node (X) the relative distance from that node to the end node (E):
// d(X,E) = d(S,E) - d(S,X)
for(int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
RaceTile tile = map[row][col];
if(tile.isEmpty()) {
tile.setDistanceToEnd(end.getDistance() - tile.getDistance());
}
}
}

// Now calculate the costs when cheating
Set<Vector2> movements = movements(cheatTime);

for(RaceTile tile : pathTiles) {
cheat(tile, movements);
}
Expand Down Expand Up @@ -204,7 +206,7 @@ private Set<RaceTile> getAdjacents(RaceTile cell) {
if(isInbounds(newPos.getY(), newPos.getX())) {
RaceTile newCell = map[newPos.getY()][newPos.getX()];

if (!newCell.isWall() && !newCell.isVisited()) {
if (newCell.isEmpty() && !newCell.isVisited()) {
adjacents.add(newCell);
}
}
Expand Down
25 changes: 1 addition & 24 deletions src/main/java/com/adventofcode/flashk/day20/RaceTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
public class RaceTile implements Comparable<RaceTile> {

private static final char WALL = '#';
private static final char EMPTY = '.';
private static final char START = 'S';
private static final char END = 'E';

Expand All @@ -22,27 +21,13 @@ public class RaceTile implements Comparable<RaceTile> {
private boolean visited = false;
private long distance = Long.MAX_VALUE;
private long distanceToEnd = Long.MAX_VALUE;
private boolean cheat = false;
private RaceTile parent;

public RaceTile(Vector2 position, char value) {
this.position = position;
this.value = value;
}

public void cheat() {
value = EMPTY;
cheat = true;
}

public void reset(){
visited = false;
distance = Long.MAX_VALUE;
value = cheat ? WALL : value;
cheat = false;
parent = null;
}

public boolean isStart() {
return value == START;
}
Expand All @@ -52,15 +37,7 @@ public boolean isEnd() {
}

public boolean isEmpty() {
return !isWall();
}

public boolean isWall() {
return value == WALL;
}

public void paint() {
System.out.print(value);
return value != WALL;
}

@Override
Expand Down

0 comments on commit 982e1f2

Please sign in to comment.