Skip to content

Commit ea494e2

Browse files
committed
day16 part 1 save
1 parent 06264d9 commit ea494e2

File tree

1 file changed

+21
-13
lines changed
  • src/main/java/net/leibi/adventofcode2023/day16

1 file changed

+21
-13
lines changed

src/main/java/net/leibi/adventofcode2023/day16/Day16.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import net.leibi.helpers.InputHelper;
55

66
import java.util.HashMap;
7+
import java.util.HashSet;
78
import java.util.Map;
9+
import java.util.Set;
810

911

1012
@Slf4j
@@ -15,6 +17,8 @@ public class Day16 {
1517
private static char[][] charMatrixFromInput;
1618
private static char[][] energizedCharMatrix;
1719

20+
private static final Set<DirectedPoint> alreadySeenDirectedPoints = new HashSet<>();
21+
1822
public static long day1(String input) {
1923

2024
charMatrixFromInput = InputHelper.getCharMatrixFromInput(input);
@@ -68,15 +72,14 @@ static Map<MovingDecisionKey, MovingDirection> getMovingDirectionsMap() {
6872
}
6973

7074
static MovingDirection getMovingDirection(MovingDirection direction, Character device) {
71-
log.info("Getting moving direction on device {} with direction {}", device, direction);
72-
return switch (device) {
73-
case '.' -> direction;
74-
default -> movingDirectionsMap.get(new MovingDecisionKey(direction, device));
75-
};
75+
if (device == '.') return direction;
76+
return movingDirectionsMap.get(new MovingDecisionKey(direction, device));
7677
}
7778

7879
private static void followBeam(DirectedPoint directedPoint) {
7980
if (directedPoint == null || directedPoint.point == null) return;
81+
if(alreadySeenDirectedPoints.contains(directedPoint)) return;
82+
alreadySeenDirectedPoints.add(directedPoint);
8083
log.info("Following beam: {}", directedPoint);
8184
var nextMovingDirection = getNewMovingDirection(directedPoint);
8285
final var nextPoints = directedPoint.point.getNextPoint(nextMovingDirection);
@@ -87,7 +90,11 @@ private static void followBeam(DirectedPoint directedPoint) {
8790
}
8891

8992
private static MovingDirection getNewMovingDirection(DirectedPoint directedPoint) {
90-
if (directedPoint.point == null) return directedPoint.movingDirection;
93+
if (directedPoint.point == null)
94+
{
95+
log.info("Dore");
96+
return directedPoint.movingDirection;
97+
}
9198
var device = getDevice(directedPoint);
9299
return getMovingDirection(directedPoint.movingDirection, device);
93100
}
@@ -112,20 +119,21 @@ record MovingDecisionKey(MovingDirection movingDirection, Character Device) {
112119
record Point(int x, int y) {
113120
Point {
114121
if (x < 0 || y < 0 || x >= charMatrixFromInput.length || y >= charMatrixFromInput[0].length) {
122+
log.info("Not a good point {},{}",x,y);
115123
throw new IllegalArgumentException();
116124
}
117125
}
118126

119127
NextDirections getNextPoint(MovingDirection movingDirection) {
120128
return switch (movingDirection) {
121-
case RIGHT -> new NextDirections(new DirectedPoint(x , y+1, MovingDirection.RIGHT), null);
122-
case LEFT -> new NextDirections(new DirectedPoint(x , y-1, MovingDirection.LEFT), null);
123-
case UP -> new NextDirections(new DirectedPoint(x-1, y , MovingDirection.UP), null);
124-
case DOWN -> new NextDirections(new DirectedPoint(x+1, y , MovingDirection.DOWN), null);
129+
case RIGHT -> new NextDirections(new DirectedPoint(x, y + 1, MovingDirection.RIGHT), null);
130+
case LEFT -> new NextDirections(new DirectedPoint(x, y - 1, MovingDirection.LEFT), null);
131+
case UP -> new NextDirections(new DirectedPoint(x - 1, y, MovingDirection.UP), null);
132+
case DOWN -> new NextDirections(new DirectedPoint(x + 1, y, MovingDirection.DOWN), null);
125133
case UPANDDOWN ->
126-
new NextDirections(new DirectedPoint(x+1, y , MovingDirection.DOWN), new DirectedPoint(x-1, y , MovingDirection.UP));
134+
new NextDirections(new DirectedPoint(x + 1, y, MovingDirection.DOWN), new DirectedPoint(x - 1, y, MovingDirection.UP));
127135
case LEFTANDRIGHT ->
128-
new NextDirections(new DirectedPoint(x , y+1, MovingDirection.LEFT), new DirectedPoint(x , y+1, MovingDirection.RIGHT));
136+
new NextDirections(new DirectedPoint(x, y + 1, MovingDirection.LEFT), new DirectedPoint(x, y + 1, MovingDirection.RIGHT));
129137
};
130138
}
131139

@@ -141,7 +149,7 @@ public DirectedPoint(int x, int y, MovingDirection movingDirection) {
141149
try {
142150
p = new Point(x, y);
143151
} catch (IllegalArgumentException e) {
144-
log.info("Point hitting a wall: {},{}", x,y);
152+
log.info("Point hitting a wall: {},{}", x, y);
145153
}
146154
this(p, movingDirection);
147155
}

0 commit comments

Comments
 (0)