4
4
import net .leibi .helpers .InputHelper ;
5
5
6
6
import java .util .HashMap ;
7
+ import java .util .HashSet ;
7
8
import java .util .Map ;
9
+ import java .util .Set ;
8
10
9
11
10
12
@ Slf4j
@@ -15,6 +17,8 @@ public class Day16 {
15
17
private static char [][] charMatrixFromInput ;
16
18
private static char [][] energizedCharMatrix ;
17
19
20
+ private static final Set <DirectedPoint > alreadySeenDirectedPoints = new HashSet <>();
21
+
18
22
public static long day1 (String input ) {
19
23
20
24
charMatrixFromInput = InputHelper .getCharMatrixFromInput (input );
@@ -68,15 +72,14 @@ static Map<MovingDecisionKey, MovingDirection> getMovingDirectionsMap() {
68
72
}
69
73
70
74
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 ));
76
77
}
77
78
78
79
private static void followBeam (DirectedPoint directedPoint ) {
79
80
if (directedPoint == null || directedPoint .point == null ) return ;
81
+ if (alreadySeenDirectedPoints .contains (directedPoint )) return ;
82
+ alreadySeenDirectedPoints .add (directedPoint );
80
83
log .info ("Following beam: {}" , directedPoint );
81
84
var nextMovingDirection = getNewMovingDirection (directedPoint );
82
85
final var nextPoints = directedPoint .point .getNextPoint (nextMovingDirection );
@@ -87,7 +90,11 @@ private static void followBeam(DirectedPoint directedPoint) {
87
90
}
88
91
89
92
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
+ }
91
98
var device = getDevice (directedPoint );
92
99
return getMovingDirection (directedPoint .movingDirection , device );
93
100
}
@@ -112,20 +119,21 @@ record MovingDecisionKey(MovingDirection movingDirection, Character Device) {
112
119
record Point (int x , int y ) {
113
120
Point {
114
121
if (x < 0 || y < 0 || x >= charMatrixFromInput .length || y >= charMatrixFromInput [0 ].length ) {
122
+ log .info ("Not a good point {},{}" ,x ,y );
115
123
throw new IllegalArgumentException ();
116
124
}
117
125
}
118
126
119
127
NextDirections getNextPoint (MovingDirection movingDirection ) {
120
128
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 );
125
133
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 ));
127
135
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 ));
129
137
};
130
138
}
131
139
@@ -141,7 +149,7 @@ public DirectedPoint(int x, int y, MovingDirection movingDirection) {
141
149
try {
142
150
p = new Point (x , y );
143
151
} catch (IllegalArgumentException e ) {
144
- log .info ("Point hitting a wall: {},{}" , x ,y );
152
+ log .info ("Point hitting a wall: {},{}" , x , y );
145
153
}
146
154
this (p , movingDirection );
147
155
}
0 commit comments