Skip to content

Commit

Permalink
Refactory day 16 puzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 16, 2023
1 parent fd5efc7 commit 2ade6ea
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 171 deletions.
9 changes: 2 additions & 7 deletions src/main/java/com/adventofcode/flashk/day16/EmptyTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@

public class EmptyTile extends Tile {

private static final char EMPTY = '.';

protected EmptyTile(char value, int row, int col) {
super(value, row, col);
if(value != EMPTY) {
throw new IllegalArgumentException("Value is not an empty tile: '.'");
}
protected EmptyTile(int row, int col) {
super(row, col);
}

@Override
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/adventofcode/flashk/day16/MirrorLeftTile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.adventofcode.flashk.day16;

import com.adventofcode.flashk.common.Vector2;

import java.util.List;

public class MirrorLeftTile extends Tile {

public MirrorLeftTile(int row, int col) {
super(row, col);
}

@Override
public List<Vector2> nextDirections(Vector2 direction) {

Vector2 newDirection = new Vector2(direction);

if(RIGHT_DIRECTION.equals(direction) || LEFT_DIRECTION.equals(direction)) {
newDirection.rotateLeft();
} else {
newDirection.rotateRight();
}

return List.of(newDirection);

}

}
27 changes: 27 additions & 0 deletions src/main/java/com/adventofcode/flashk/day16/MirrorRightTile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.adventofcode.flashk.day16;

import com.adventofcode.flashk.common.Vector2;

import java.util.List;

public class MirrorRightTile extends Tile {

public MirrorRightTile(int row, int col) {
super(row, col);
}

@Override
public List<Vector2> nextDirections(Vector2 direction) {

Vector2 newDirection = new Vector2(direction);

if(RIGHT_DIRECTION.equals(direction) || LEFT_DIRECTION.equals(direction)) {
newDirection.rotateRight();
} else {
newDirection.rotateLeft();
}

return List.of(newDirection);
}

}
21 changes: 0 additions & 21 deletions src/main/java/com/adventofcode/flashk/day16/MirrorTile.java

This file was deleted.

29 changes: 0 additions & 29 deletions src/main/java/com/adventofcode/flashk/day16/MirrorTileLeft.java

This file was deleted.

29 changes: 0 additions & 29 deletions src/main/java/com/adventofcode/flashk/day16/MirrorTileRight.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.adventofcode.flashk.day16;

import com.adventofcode.flashk.common.Vector2;

import java.util.List;

public class SplitterHorizontalTile extends Tile{

public SplitterHorizontalTile(int row, int col) {
super(row, col);
}

@Override
public List<Vector2> nextDirections(Vector2 direction) {

if(RIGHT_DIRECTION.equals(direction) || LEFT_DIRECTION.equals(direction)) {
return List.of(direction);
}

return List.of(Vector2.left(), Vector2.right());

}
}
15 changes: 0 additions & 15 deletions src/main/java/com/adventofcode/flashk/day16/SplitterTile.java

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.adventofcode.flashk.day16;

import com.adventofcode.flashk.common.Vector2;

import java.util.List;

public class SplitterVerticalTile extends Tile {

public SplitterVerticalTile(int row, int col) {
super(row, col);
}

@Override
public List<Vector2> nextDirections(Vector2 direction) {

if(UP_DIRECTION.equals(direction) || DOWN_DIRECTION.equals(direction)) {
return List.of(direction);
}

return List.of(Vector2.up(), Vector2.down());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ public long solveA(){


private long bfs(Vector2 startPosition, Vector2 startingDirection) {
// Start conditions
Tile startingTile = map[startPosition.getY()][startPosition.getX()];

Queue<Tile> tilesQueue = new LinkedList<>();
Queue<Vector2> directionsQueue = new LinkedList<>();

Tile startingTile = map[startPosition.getY()][startPosition.getX()];
startingTile.visit(startingDirection);

tilesQueue.add(startingTile);
directionsQueue.add(startingDirection);

Expand Down Expand Up @@ -107,14 +108,15 @@ private Optional<Tile> nextTile(Vector2 nextDirection, Tile currentTile) {
Vector2 currentTilePos = new Vector2(currentTile.getCol(), currentTile.getRow());
Vector2 nextPosition = Vector2.transform(nextDirection, currentTilePos);

// Exclude out of bounds tiles
if(nextPosition.getX() >= 0 &&
nextPosition.getX() < cols &&
nextPosition.getY() >= 0 &&
nextPosition.getY() < rows) {

Tile nextTile = map[nextPosition.getY()][nextPosition.getX()];

// Next tile must NOT have been visited from that direction
// Exclude tiles that have been already visited from that direction
if(!nextTile.isVisited(nextDirection)) {
return Optional.of(nextTile);
}
Expand All @@ -139,11 +141,11 @@ private long countVisitedTiles() {
private Tile buildTile(char value, int row, int col) {

return switch (value) {
case '.': yield new EmptyTile(value, row, col);
case '\\': yield new MirrorTileLeft(value, row, col);
case '/': yield new MirrorTileRight(value, row, col);
case '|': yield new SplitterTileVertical(value, row, col);
case '-': yield new SplitterTileHorizontal(value, row, col);
case '.': yield new EmptyTile(row, col);
case '\\': yield new MirrorLeftTile(row, col);
case '/': yield new MirrorRightTile(row, col);
case '|': yield new SplitterVerticalTile(row, col);
case '-': yield new SplitterHorizontalTile(row, col);
default: throw new IllegalArgumentException("Unexpected value: "+value);
};
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/adventofcode/flashk/day16/Tile.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;
import java.util.Set;

@Getter
@Getter
public abstract class Tile {

// Possible directions in tiles
Expand All @@ -16,13 +16,11 @@ public abstract class Tile {
public static final Vector2 UP_DIRECTION = Vector2.down();
public static final Vector2 DOWN_DIRECTION = Vector2.up();

private final char value;
private final int row;
private final int col;
private final Set<Vector2> visitedDirections = new HashSet<>();

protected Tile(char value, int row, int col) {
this.value = value;
protected Tile(int row, int col) {
this.row = row;
this.col = col;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

class MirrorTileTest {

private MirrorTile left = new MirrorTileLeft('\\',1,1);
private MirrorTile right = new MirrorTileRight('/',1,1);
private final Tile left = new MirrorLeftTile(1,1);
private final Tile right = new MirrorRightTile(1,1);

@Test
void reflectLeftMirrorWithRightDirection() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

class SplitterTileTest {

private SplitterTile vertical = new SplitterTileVertical('|',1,1);
private SplitterTile horizontal = new SplitterTileHorizontal('-',1,1);
private final Tile vertical = new SplitterVerticalTile(1,1);
private final Tile horizontal = new SplitterHorizontalTile(1,1);

@Test
void nextDirectionsSplitterTileVerticalLeft() {
Expand Down

0 comments on commit 2ade6ea

Please sign in to comment.