From 2ade6ea6828939172f0c7801e1f43674ced8de1b Mon Sep 17 00:00:00 2001 From: Flashk Date: Sat, 16 Dec 2023 10:32:47 +0100 Subject: [PATCH] Refactory day 16 puzzle --- .../adventofcode/flashk/day16/EmptyTile.java | 9 ++---- .../flashk/day16/MirrorLeftTile.java | 28 ++++++++++++++++++ .../flashk/day16/MirrorRightTile.java | 27 +++++++++++++++++ .../adventofcode/flashk/day16/MirrorTile.java | 21 -------------- .../flashk/day16/MirrorTileLeft.java | 29 ------------------- .../flashk/day16/MirrorTileRight.java | 29 ------------------- .../flashk/day16/SplitterHorizontalTile.java | 23 +++++++++++++++ .../flashk/day16/SplitterTile.java | 15 ---------- .../flashk/day16/SplitterTileHorizontal.java | 28 ------------------ .../flashk/day16/SplitterTileVertical.java | 26 ----------------- .../flashk/day16/SplitterVerticalTile.java | 22 ++++++++++++++ .../flashk/day16/TheFloorWillBeLava.java | 18 +++++++----- .../com/adventofcode/flashk/day16/Tile.java | 6 ++-- .../flashk/day16/MirrorTileTest.java | 4 +-- .../flashk/day16/SplitterTileTest.java | 4 +-- 15 files changed, 118 insertions(+), 171 deletions(-) create mode 100644 src/main/java/com/adventofcode/flashk/day16/MirrorLeftTile.java create mode 100644 src/main/java/com/adventofcode/flashk/day16/MirrorRightTile.java delete mode 100644 src/main/java/com/adventofcode/flashk/day16/MirrorTile.java delete mode 100644 src/main/java/com/adventofcode/flashk/day16/MirrorTileLeft.java delete mode 100644 src/main/java/com/adventofcode/flashk/day16/MirrorTileRight.java create mode 100644 src/main/java/com/adventofcode/flashk/day16/SplitterHorizontalTile.java delete mode 100644 src/main/java/com/adventofcode/flashk/day16/SplitterTile.java delete mode 100644 src/main/java/com/adventofcode/flashk/day16/SplitterTileHorizontal.java delete mode 100644 src/main/java/com/adventofcode/flashk/day16/SplitterTileVertical.java create mode 100644 src/main/java/com/adventofcode/flashk/day16/SplitterVerticalTile.java diff --git a/src/main/java/com/adventofcode/flashk/day16/EmptyTile.java b/src/main/java/com/adventofcode/flashk/day16/EmptyTile.java index 1ff856e..1829450 100644 --- a/src/main/java/com/adventofcode/flashk/day16/EmptyTile.java +++ b/src/main/java/com/adventofcode/flashk/day16/EmptyTile.java @@ -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 diff --git a/src/main/java/com/adventofcode/flashk/day16/MirrorLeftTile.java b/src/main/java/com/adventofcode/flashk/day16/MirrorLeftTile.java new file mode 100644 index 0000000..e024049 --- /dev/null +++ b/src/main/java/com/adventofcode/flashk/day16/MirrorLeftTile.java @@ -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 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); + + } + +} diff --git a/src/main/java/com/adventofcode/flashk/day16/MirrorRightTile.java b/src/main/java/com/adventofcode/flashk/day16/MirrorRightTile.java new file mode 100644 index 0000000..72759d5 --- /dev/null +++ b/src/main/java/com/adventofcode/flashk/day16/MirrorRightTile.java @@ -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 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); + } + +} diff --git a/src/main/java/com/adventofcode/flashk/day16/MirrorTile.java b/src/main/java/com/adventofcode/flashk/day16/MirrorTile.java deleted file mode 100644 index 54abf53..0000000 --- a/src/main/java/com/adventofcode/flashk/day16/MirrorTile.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.adventofcode.flashk.day16; - -import com.adventofcode.flashk.common.Vector2; -import lombok.Getter; - -import java.util.List; - -@Getter -public abstract class MirrorTile extends Tile { - - protected MirrorTile(char value, int row, int col) { - super(value, row, col); - } - - @Override - public List nextDirections(Vector2 direction) { - return List.of(reflect(direction)); - } - - abstract Vector2 reflect(Vector2 direction); -} diff --git a/src/main/java/com/adventofcode/flashk/day16/MirrorTileLeft.java b/src/main/java/com/adventofcode/flashk/day16/MirrorTileLeft.java deleted file mode 100644 index 25a4e11..0000000 --- a/src/main/java/com/adventofcode/flashk/day16/MirrorTileLeft.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.adventofcode.flashk.day16; - -import com.adventofcode.flashk.common.Vector2; - -public class MirrorTileLeft extends MirrorTile { - - private static final char LEFT = '\\'; - - public MirrorTileLeft(char value, int row, int col) { - super(value, row, col); - if(value != LEFT) { - throw new IllegalArgumentException("Value is not a left mirror: '\\'"); - } - } - - @Override - protected Vector2 reflect(Vector2 direction) { - - Vector2 newDirection = new Vector2(direction); - - if(RIGHT_DIRECTION.equals(direction) || LEFT_DIRECTION.equals(direction)) { - newDirection.rotateLeft(); - return newDirection; - } - - newDirection.rotateRight(); - return newDirection; - } -} diff --git a/src/main/java/com/adventofcode/flashk/day16/MirrorTileRight.java b/src/main/java/com/adventofcode/flashk/day16/MirrorTileRight.java deleted file mode 100644 index a00c53a..0000000 --- a/src/main/java/com/adventofcode/flashk/day16/MirrorTileRight.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.adventofcode.flashk.day16; - -import com.adventofcode.flashk.common.Vector2; - -public class MirrorTileRight extends MirrorTile { - - private static final char RIGHT = '/'; - - protected MirrorTileRight(char value, int row, int col) { - super(value, row, col); - if(value != RIGHT) { - throw new IllegalArgumentException("Value is not a left mirror: '\\'"); - } - } - - @Override - protected Vector2 reflect(Vector2 direction) { - - Vector2 newDirection = new Vector2(direction); - - if(RIGHT_DIRECTION.equals(direction) || LEFT_DIRECTION.equals(direction)) { - newDirection.rotateRight(); - return newDirection; - } - - newDirection.rotateLeft(); - return newDirection; - } -} diff --git a/src/main/java/com/adventofcode/flashk/day16/SplitterHorizontalTile.java b/src/main/java/com/adventofcode/flashk/day16/SplitterHorizontalTile.java new file mode 100644 index 0000000..4bf5351 --- /dev/null +++ b/src/main/java/com/adventofcode/flashk/day16/SplitterHorizontalTile.java @@ -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 nextDirections(Vector2 direction) { + + if(RIGHT_DIRECTION.equals(direction) || LEFT_DIRECTION.equals(direction)) { + return List.of(direction); + } + + return List.of(Vector2.left(), Vector2.right()); + + } +} diff --git a/src/main/java/com/adventofcode/flashk/day16/SplitterTile.java b/src/main/java/com/adventofcode/flashk/day16/SplitterTile.java deleted file mode 100644 index c3b8382..0000000 --- a/src/main/java/com/adventofcode/flashk/day16/SplitterTile.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.adventofcode.flashk.day16; - -public abstract class SplitterTile extends Tile { - - private static final char VERTICAL = '|'; - private static final char HORIZONTAL = '-'; - - protected SplitterTile(char value, int row, int col) { - super(value, row, col); - if(value != VERTICAL && value != HORIZONTAL) { - throw new IllegalArgumentException("Tile must be a splitter"); - } - } - -} diff --git a/src/main/java/com/adventofcode/flashk/day16/SplitterTileHorizontal.java b/src/main/java/com/adventofcode/flashk/day16/SplitterTileHorizontal.java deleted file mode 100644 index 79ad3b2..0000000 --- a/src/main/java/com/adventofcode/flashk/day16/SplitterTileHorizontal.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.adventofcode.flashk.day16; - -import com.adventofcode.flashk.common.Vector2; - -import java.util.List; - -public class SplitterTileHorizontal extends SplitterTile{ - - private static final char HORIZONTAL = '-'; - - protected SplitterTileHorizontal(char value, int row, int col) { - super(value, row, col); - if(value != HORIZONTAL) { - throw new IllegalArgumentException("Value is not a horizontal splitter: '-'"); - } - } - - @Override - List nextDirections(Vector2 direction) { - - if(RIGHT_DIRECTION.equals(direction) || LEFT_DIRECTION.equals(direction)) { - return List.of(direction); - } - - return List.of(Vector2.left(), Vector2.right()); - - } -} diff --git a/src/main/java/com/adventofcode/flashk/day16/SplitterTileVertical.java b/src/main/java/com/adventofcode/flashk/day16/SplitterTileVertical.java deleted file mode 100644 index 7cf1049..0000000 --- a/src/main/java/com/adventofcode/flashk/day16/SplitterTileVertical.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.adventofcode.flashk.day16; - -import com.adventofcode.flashk.common.Vector2; - -import java.util.List; - -public class SplitterTileVertical extends SplitterTile { - - private static final char VERTICAL = '|'; - - protected SplitterTileVertical(char value, int row, int col) { - super(value, row, col); - if(value != VERTICAL) { - throw new IllegalArgumentException("Value is not a vertical splitter: '|'"); - } - } - - @Override - List nextDirections(Vector2 direction) { - if(UP_DIRECTION.equals(direction) || DOWN_DIRECTION.equals(direction)) { - return List.of(direction); - } - - return List.of(Vector2.up(), Vector2.down()); - } -} diff --git a/src/main/java/com/adventofcode/flashk/day16/SplitterVerticalTile.java b/src/main/java/com/adventofcode/flashk/day16/SplitterVerticalTile.java new file mode 100644 index 0000000..c4b9b99 --- /dev/null +++ b/src/main/java/com/adventofcode/flashk/day16/SplitterVerticalTile.java @@ -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 nextDirections(Vector2 direction) { + + if(UP_DIRECTION.equals(direction) || DOWN_DIRECTION.equals(direction)) { + return List.of(direction); + } + + return List.of(Vector2.up(), Vector2.down()); + } +} diff --git a/src/main/java/com/adventofcode/flashk/day16/TheFloorWillBeLava.java b/src/main/java/com/adventofcode/flashk/day16/TheFloorWillBeLava.java index fbe9202..48d33f4 100644 --- a/src/main/java/com/adventofcode/flashk/day16/TheFloorWillBeLava.java +++ b/src/main/java/com/adventofcode/flashk/day16/TheFloorWillBeLava.java @@ -31,12 +31,13 @@ public long solveA(){ private long bfs(Vector2 startPosition, Vector2 startingDirection) { - // Start conditions - Tile startingTile = map[startPosition.getY()][startPosition.getX()]; Queue tilesQueue = new LinkedList<>(); Queue directionsQueue = new LinkedList<>(); + + Tile startingTile = map[startPosition.getY()][startPosition.getX()]; startingTile.visit(startingDirection); + tilesQueue.add(startingTile); directionsQueue.add(startingDirection); @@ -107,6 +108,7 @@ private Optional 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 && @@ -114,7 +116,7 @@ private Optional nextTile(Vector2 nextDirection, Tile currentTile) { 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); } @@ -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); }; } diff --git a/src/main/java/com/adventofcode/flashk/day16/Tile.java b/src/main/java/com/adventofcode/flashk/day16/Tile.java index a1e2b54..f88ed1c 100644 --- a/src/main/java/com/adventofcode/flashk/day16/Tile.java +++ b/src/main/java/com/adventofcode/flashk/day16/Tile.java @@ -7,7 +7,7 @@ import java.util.List; import java.util.Set; -@Getter + @Getter public abstract class Tile { // Possible directions in tiles @@ -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 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; } diff --git a/src/test/java/com/adventofcode/flashk/day16/MirrorTileTest.java b/src/test/java/com/adventofcode/flashk/day16/MirrorTileTest.java index f7b9ba2..3f1df5c 100644 --- a/src/test/java/com/adventofcode/flashk/day16/MirrorTileTest.java +++ b/src/test/java/com/adventofcode/flashk/day16/MirrorTileTest.java @@ -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() { diff --git a/src/test/java/com/adventofcode/flashk/day16/SplitterTileTest.java b/src/test/java/com/adventofcode/flashk/day16/SplitterTileTest.java index 52e2664..efd062b 100644 --- a/src/test/java/com/adventofcode/flashk/day16/SplitterTileTest.java +++ b/src/test/java/com/adventofcode/flashk/day16/SplitterTileTest.java @@ -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() {