Skip to content

Commit

Permalink
Day 10 - Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
kocmana committed Jan 18, 2024
1 parent 52d05b0 commit 2ec0bb5
Show file tree
Hide file tree
Showing 9 changed files with 445 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Solutions for the https://adventofcode.com/2023/[advent of code] of 2023.
|07| |
|08|👍|👍*
|09|👍|👍
|10| |
|10|👍|
|11| |
|12| |
|13| |
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/at/kocmana/aoc23/dec10/Agent.java
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 +
'}';
}
}
11 changes: 11 additions & 0 deletions src/main/java/at/kocmana/aoc23/dec10/Coordinate.java
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);
}

}
17 changes: 17 additions & 0 deletions src/main/java/at/kocmana/aoc23/dec10/Direction.java
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;
};
}
}
70 changes: 70 additions & 0 deletions src/main/java/at/kocmana/aoc23/dec10/PipeMaze.java
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()];
}

}
46 changes: 46 additions & 0 deletions src/main/java/at/kocmana/aoc23/dec10/PipeType.java
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;
}
}
54 changes: 54 additions & 0 deletions src/main/java/at/kocmana/aoc23/dec10/Vector.java
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 +
'}';
}
}
Loading

0 comments on commit 2ec0bb5

Please sign in to comment.