5
5
import dev .linl33 .adventofcode .lib .grid .Grid ;
6
6
import dev .linl33 .adventofcode .lib .grid .GridVisitResult ;
7
7
import dev .linl33 .adventofcode .lib .point .Point2D ;
8
+ import org .intellij .lang .annotations .MagicConstant ;
8
9
9
10
import java .util .Arrays ;
10
11
import java .util .List ;
@@ -16,13 +17,17 @@ public enum TurningDirection {
16
17
RIGHT_90 , LEFT_90 , RIGHT_270 , LEFT_270 , INVERT , NULL
17
18
}
18
19
20
+ @ MagicConstant (intValues = {HEADING_NORTH , HEADING_EAST , HEADING_SOUTH , HEADING_WEST })
21
+ public @interface GridHeading {}
22
+
19
23
public static final int HEADING_NORTH = 0 ;
20
24
public static final int HEADING_EAST = 1 ;
21
25
public static final int HEADING_SOUTH = 2 ;
22
26
public static final int HEADING_WEST = 3 ;
23
27
public static final int [] HEADINGS = new int []{HEADING_NORTH , HEADING_EAST , HEADING_SOUTH , HEADING_WEST };
24
28
25
- public static int turn (TurningDirection direction , int heading ) {
29
+ @ GridHeading
30
+ public static int turn (TurningDirection direction , @ GridHeading int heading ) {
26
31
return switch (direction ) {
27
32
case RIGHT_90 , LEFT_270 -> HEADINGS [(heading + 1 ) % 4 ];
28
33
case LEFT_90 , RIGHT_270 -> HEADINGS [Math .floorMod (heading - 1 , 4 )];
@@ -31,24 +36,24 @@ public static int turn(TurningDirection direction, int heading) {
31
36
};
32
37
}
33
38
34
- public static Point2D move (Point2D curr , int heading ) {
39
+ public static Point2D move (Point2D curr , @ GridHeading int heading ) {
35
40
return move (curr , heading , false , false );
36
41
}
37
42
38
- public static Point2D move (Point2D curr , int heading , boolean invertX , boolean invertY ) {
43
+ public static Point2D move (Point2D curr , @ GridHeading int heading , boolean invertX , boolean invertY ) {
39
44
return move (curr , heading , 1 , invertX , invertY );
40
45
}
41
46
42
47
public static <T extends GridEntity & HasHeading > Point2D move (T entity , boolean invertX , boolean invertY ) {
43
48
return move (entity .getPosition (), entity .getHeading (), invertX , invertY );
44
49
}
45
50
46
- public static Point2D move (Point2D curr , int heading , int units , boolean invertX , boolean invertY ) {
47
- if (invertX && (heading == 1 || heading == 3 )) {
51
+ public static Point2D move (Point2D curr , @ GridHeading int heading , int units , boolean invertX , boolean invertY ) {
52
+ if (invertX && (heading == HEADING_EAST || heading == HEADING_WEST )) {
48
53
heading = turn (TurningDirection .INVERT , heading );
49
54
}
50
55
51
- if (invertY && (heading == 0 || heading == 2 )) {
56
+ if (invertY && (heading == HEADING_NORTH || heading == HEADING_SOUTH )) {
52
57
heading = turn (TurningDirection .INVERT , heading );
53
58
}
54
59
@@ -61,6 +66,7 @@ public static Point2D move(Point2D curr, int heading, int units, boolean invertX
61
66
};
62
67
}
63
68
69
+ @ GridHeading
64
70
public static int parseHeading (char headingChar ) {
65
71
return switch (headingChar ) {
66
72
case '^' -> HEADING_NORTH ;
@@ -71,7 +77,7 @@ public static int parseHeading(char headingChar) {
71
77
};
72
78
}
73
79
74
- public static char headingToChar (int heading ) {
80
+ public static char headingToChar (@ GridHeading int heading ) {
75
81
return switch (heading ) {
76
82
case HEADING_NORTH -> '^' ;
77
83
case HEADING_EAST -> '>' ;
0 commit comments