-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
445 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package at.kocmana.aoc23.dec10; | ||
|
||
public class Agent { | ||
|
||
private Coordinate position; | ||
private Vector currentMovingDirection; | ||
|
||
public Agent(Coordinate position, Vector currentMovingDirection) { | ||
this.position = position; | ||
this.currentMovingDirection = currentMovingDirection; | ||
} | ||
|
||
public void move(Vector vector) { | ||
this.position = Coordinate.of( | ||
position.x() + vector.getX(), | ||
position.y() + vector.getY() | ||
); | ||
this.currentMovingDirection = vector; | ||
} | ||
|
||
public void move(Direction direction) { | ||
move(Vector.fromDirection(direction)); | ||
} | ||
|
||
public Coordinate getPosition() { | ||
return position; | ||
} | ||
|
||
public Direction getCurrentMovingDirection() { | ||
return currentMovingDirection.toDirection(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Agent{" + | ||
"position=" + position + | ||
", currentMovingDirection=" + currentMovingDirection + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package at.kocmana.aoc23.dec10; | ||
|
||
public record Coordinate( | ||
int x, | ||
int y) { | ||
|
||
static Coordinate of(int x, int y) { | ||
return new Coordinate(x, y); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package at.kocmana.aoc23.dec10; | ||
|
||
public enum Direction { | ||
NORTH, | ||
EAST, | ||
SOUTH, | ||
WEST; | ||
|
||
public Direction getOpposite() { | ||
return switch (this) { | ||
case NORTH -> SOUTH; | ||
case EAST -> WEST; | ||
case SOUTH -> NORTH; | ||
case WEST -> EAST; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package at.kocmana.aoc23.dec10; | ||
|
||
import static java.util.Objects.nonNull; | ||
|
||
import at.kocmana.aoc23.common.ResourceToString; | ||
import at.kocmana.aoc23.common.Tuple; | ||
import java.util.Arrays; | ||
|
||
public class PipeMaze { | ||
|
||
private final PipeType[][] maze; | ||
|
||
public PipeMaze(PipeType[][] maze) { | ||
this.maze = maze; | ||
} | ||
|
||
public static void main(String[] args) { | ||
var puzzleInput = ResourceToString.from("dec10", "PipeMaze.txt"); | ||
var maze = parseMaze(puzzleInput); | ||
System.out.printf("Result: %d%n", maze.walkRound()); | ||
} | ||
|
||
public static PipeMaze parseMaze(String input) { | ||
var maze = input.lines() | ||
.map(PipeMaze::parseLine) | ||
.toArray(PipeType[][]::new); | ||
|
||
return new PipeMaze(maze); | ||
} | ||
|
||
private static PipeType[] parseLine(String input) { | ||
return Arrays.stream(input.split("")) | ||
.map(PipeType::fromString) | ||
.toArray(PipeType[]::new); | ||
} | ||
|
||
int walkRound() { | ||
var start = findStartPoint(); | ||
var startDirection = Direction.EAST; | ||
|
||
var agent = new Agent(start, Vector.fromDirection(startDirection)); | ||
var distanceCounter = 0; | ||
do { | ||
var pipe = getCoordinate(agent.getPosition()); | ||
var originatingDirection = agent.getCurrentMovingDirection().getOpposite(); | ||
var resultingDirection = pipe.traverse(originatingDirection); | ||
agent.move(resultingDirection); | ||
distanceCounter++; | ||
} while (!agent.getPosition().equals(start)); | ||
|
||
return distanceCounter / 2; | ||
} | ||
|
||
Coordinate findStartPoint() { | ||
for (int i = 0; i < maze.length; i++) { | ||
for (int j = 0; j < maze[i].length; j++) { | ||
var pipeType = maze[i][j]; | ||
if (nonNull(pipeType) && pipeType.equals(PipeType.START)) { | ||
return Coordinate.of(j, i); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
PipeType getCoordinate(Coordinate coordinate) { | ||
return maze[coordinate.y()][coordinate.x()]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package at.kocmana.aoc23.dec10; | ||
|
||
import java.util.Arrays; | ||
|
||
public enum PipeType { | ||
VERTICAL("|", Direction.NORTH, Direction.SOUTH), | ||
HORIZONTAL("-", Direction.EAST, Direction.WEST), | ||
NORTH_TO_EAST("L", Direction.NORTH, Direction.EAST), | ||
NORTH_TO_WEST("J", Direction.NORTH, Direction.WEST), | ||
SOUTH_TO_WEST("7", Direction.SOUTH, Direction.WEST), | ||
SOUTH_TO_EAST("F", Direction.SOUTH, Direction.EAST), | ||
START("S", Direction.EAST, Direction.EAST); | ||
|
||
private final String stringRepresentation; | ||
private final Direction firstDirection; | ||
private final Direction secondDirection; | ||
|
||
PipeType(String stringRepresentation, Direction firstDirection, Direction secondDirection) { | ||
this.stringRepresentation = stringRepresentation; | ||
this.firstDirection = firstDirection; | ||
this.secondDirection = secondDirection; | ||
} | ||
|
||
static PipeType fromString(String string) { | ||
return Arrays.stream(PipeType.values()) | ||
.filter(pipeType -> pipeType.stringRepresentation.equalsIgnoreCase(string)) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
public boolean canTraverse(Direction direction) { | ||
return direction.equals(firstDirection.getOpposite()) | ||
|| direction.equals(secondDirection.getOpposite()); | ||
} | ||
|
||
public Direction traverse(Direction sourceDirection) { | ||
return sourceDirection.equals(firstDirection) | ||
? secondDirection | ||
: firstDirection; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.stringRepresentation; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package at.kocmana.aoc23.dec10; | ||
|
||
import java.util.Map; | ||
import java.util.function.IntPredicate; | ||
|
||
public class Vector { | ||
|
||
private final int x; | ||
private final int y; | ||
|
||
static final IntPredicate IS_VALID_VECTOR_VALUE = value -> value == 0 || Math.abs(value) == 1; | ||
static final Map<Direction, Vector> VECTOR_MAP = Map.of( | ||
Direction.NORTH, new Vector(0, -1), | ||
Direction.EAST, new Vector(1, 0), | ||
Direction.SOUTH, new Vector(0, 1), | ||
Direction.WEST, new Vector(-1, 0) | ||
); | ||
|
||
private Vector(int x, int y) { | ||
if (!IS_VALID_VECTOR_VALUE.test(x) || !IS_VALID_VECTOR_VALUE.test(y)) { | ||
throw new IllegalArgumentException("Must only consist of 0 or 1"); | ||
} | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
public static Vector fromDirection(Direction direction) { | ||
return VECTOR_MAP.get(direction); | ||
} | ||
|
||
public Direction toDirection() { | ||
return VECTOR_MAP.entrySet().stream() | ||
.filter(entry -> entry.getValue().equals(this)) | ||
.map(Map.Entry::getKey) | ||
.findFirst() | ||
.orElseThrow(); | ||
} | ||
|
||
public int getX() { | ||
return x; | ||
} | ||
|
||
public int getY() { | ||
return y; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Vector{" + | ||
"x=" + x + | ||
", y=" + y + | ||
'}'; | ||
} | ||
} |
Oops, something went wrong.