Skip to content

Commit e9b8d64

Browse files
committed
Add @GridHeading annotation
1 parent 73cdcc9 commit e9b8d64

File tree

7 files changed

+39
-33
lines changed

7 files changed

+39
-33
lines changed

lib/src/main/java/dev/linl33/adventofcode/lib/HasHeading.java

+2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import dev.linl33.adventofcode.lib.util.GridUtil;
44

5+
@FunctionalInterface
56
public interface HasHeading {
7+
@GridUtil.GridHeading
68
int getHeading();
79

810
default char getHeadingAsChar() {

lib/src/main/java/dev/linl33/adventofcode/lib/util/GridUtil.java

+13-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import dev.linl33.adventofcode.lib.grid.Grid;
66
import dev.linl33.adventofcode.lib.grid.GridVisitResult;
77
import dev.linl33.adventofcode.lib.point.Point2D;
8+
import org.intellij.lang.annotations.MagicConstant;
89

910
import java.util.Arrays;
1011
import java.util.List;
@@ -16,13 +17,17 @@ public enum TurningDirection {
1617
RIGHT_90, LEFT_90, RIGHT_270, LEFT_270, INVERT, NULL
1718
}
1819

20+
@MagicConstant(intValues = {HEADING_NORTH, HEADING_EAST, HEADING_SOUTH, HEADING_WEST})
21+
public @interface GridHeading {}
22+
1923
public static final int HEADING_NORTH = 0;
2024
public static final int HEADING_EAST = 1;
2125
public static final int HEADING_SOUTH = 2;
2226
public static final int HEADING_WEST = 3;
2327
public static final int[] HEADINGS = new int[]{HEADING_NORTH, HEADING_EAST, HEADING_SOUTH, HEADING_WEST};
2428

25-
public static int turn(TurningDirection direction, int heading) {
29+
@GridHeading
30+
public static int turn(TurningDirection direction, @GridHeading int heading) {
2631
return switch (direction) {
2732
case RIGHT_90, LEFT_270 -> HEADINGS[(heading + 1) % 4];
2833
case LEFT_90, RIGHT_270 -> HEADINGS[Math.floorMod(heading - 1, 4)];
@@ -31,24 +36,24 @@ public static int turn(TurningDirection direction, int heading) {
3136
};
3237
}
3338

34-
public static Point2D move(Point2D curr, int heading) {
39+
public static Point2D move(Point2D curr, @GridHeading int heading) {
3540
return move(curr, heading, false, false);
3641
}
3742

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) {
3944
return move(curr, heading, 1, invertX, invertY);
4045
}
4146

4247
public static <T extends GridEntity & HasHeading> Point2D move(T entity, boolean invertX, boolean invertY) {
4348
return move(entity.getPosition(), entity.getHeading(), invertX, invertY);
4449
}
4550

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)) {
4853
heading = turn(TurningDirection.INVERT, heading);
4954
}
5055

51-
if (invertY && (heading == 0 || heading == 2)) {
56+
if (invertY && (heading == HEADING_NORTH || heading == HEADING_SOUTH)) {
5257
heading = turn(TurningDirection.INVERT, heading);
5358
}
5459

@@ -61,6 +66,7 @@ public static Point2D move(Point2D curr, int heading, int units, boolean invertX
6166
};
6267
}
6368

69+
@GridHeading
6470
public static int parseHeading(char headingChar) {
6571
return switch (headingChar) {
6672
case '^' -> HEADING_NORTH;
@@ -71,7 +77,7 @@ public static int parseHeading(char headingChar) {
7177
};
7278
}
7379

74-
public static char headingToChar(int heading) {
80+
public static char headingToChar(@GridHeading int heading) {
7581
return switch (heading) {
7682
case HEADING_NORTH -> '^';
7783
case HEADING_EAST -> '>';

year2018/src/main/java/dev/linl33/adventofcode/year2018/Day13.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ private static Cart simulateCartNoCrash(Cart cart, Point2D nextPos, char nextPos
117117
case '/', '\\' -> {
118118
GridUtil.TurningDirection turningDirection;
119119
if (nextPosChar == '/') {
120-
if (cart.getHeading() == 1 || cart.getHeading() == 3) {
120+
if (cart.getHeading() == GridUtil.HEADING_EAST || cart.getHeading() == GridUtil.HEADING_WEST) {
121121
turningDirection = GridUtil.TurningDirection.LEFT_90;
122122
} else {
123123
turningDirection = GridUtil.TurningDirection.RIGHT_90;
124124
}
125125
} else {
126-
if (cart.getHeading() == 1 || cart.getHeading() == 3) {
126+
if (cart.getHeading() == GridUtil.HEADING_EAST || cart.getHeading() == GridUtil.HEADING_WEST) {
127127
turningDirection = GridUtil.TurningDirection.RIGHT_90;
128128
} else {
129129
turningDirection = GridUtil.TurningDirection.LEFT_90;
@@ -182,9 +182,13 @@ private static void copyGridToGrid(Point2D pos, char[][] src, char[][] dest) {
182182
private static record Cart(
183183
String id,
184184
Point2D position,
185-
int heading,
185+
@GridUtil.GridHeading int heading,
186186
int intersectionState
187187
) implements GridEntity, HasHeading {
188+
@GridUtil.GridHeading
189+
public int heading() {
190+
return heading;
191+
}
188192
@Override
189193
public String getId() {
190194
return id();
@@ -200,11 +204,11 @@ public int getHeading() {
200204
return heading();
201205
}
202206

203-
public Cart(Point2D position, int heading, int intersectionState) {
207+
public Cart(Point2D position, @GridUtil.GridHeading int heading, int intersectionState) {
204208
this(position.toString(), position, heading, intersectionState);
205209
}
206210

207-
public Cart(Cart other, Point2D position, int heading) {
211+
public Cart(Cart other, Point2D position, @GridUtil.GridHeading int heading) {
208212
this(other.getId(), position, heading, other.intersectionState());
209213
}
210214

year2019/src/main/java/dev/linl33/adventofcode/year2019/Day11.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private static HashMap<Point2D, Long> paintHull(BufferedReader reader, long bgCo
3737
var vmOutput = vm.getOutput();
3838

3939
var color = new HashMap<Point2D, Long>();
40-
var heading = 0;
40+
var heading = GridUtil.HEADING_NORTH;
4141
var pos = new Point2D(0, 0);
4242

4343
while (!vm.hasHalted()) {

year2019/src/main/java/dev/linl33/adventofcode/year2019/Day17.java

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public Long part2(BufferedReader reader) throws Exception {
5555
.findAny()
5656
.orElseThrow(IllegalArgumentException::new);
5757

58+
@GridUtil.GridHeading
5859
var heading = GridUtil.parseHeading(robot.getValue());
5960
var position = robot.getKey();
6061
var path = new StringJoiner(",");

year2020/src/main/java/dev/linl33/adventofcode/year2020/Day12.java

+13-18
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public Integer part1(BufferedReader reader) throws Exception {
1717
return solve(reader, new WaypointNavInstrVisitor(new Point2D(1, 0)) {
1818
@Override
1919
public @NotNull Point2D visit(@NotNull Point2D ferry, @NotNull DirNavInstr instr) {
20-
return movePointTowardsDir(ferry, instr);
20+
return GridUtil.move(ferry, instr.heading(), instr.value, false, false);
2121
}
2222
});
2323
}
@@ -27,7 +27,7 @@ public Integer part2(BufferedReader reader) throws Exception {
2727
return solve(reader, new WaypointNavInstrVisitor(new Point2D(10, 1)) {
2828
@Override
2929
public @NotNull Point2D visit(@NotNull Point2D ferry, @NotNull DirNavInstr instr) {
30-
setWaypoint(movePointTowardsDir(getWaypoint(), instr));
30+
setWaypoint(GridUtil.move(getWaypoint(), instr.heading(), instr.value, false, false));
3131

3232
return ferry;
3333
}
@@ -46,22 +46,6 @@ private static int solve(BufferedReader reader, NavInstrVisitor visitor) {
4646
.manhattanDistance(Point.ORIGIN_2D);
4747
}
4848

49-
public static Point2D movePointTowardsDir(Point2D pt, DirNavInstr vector) {
50-
return GridUtil.move(
51-
pt,
52-
switch (vector.action) {
53-
case NORTH -> GridUtil.HEADING_NORTH;
54-
case SOUTH -> GridUtil.HEADING_SOUTH;
55-
case EAST -> GridUtil.HEADING_EAST;
56-
case WEST -> GridUtil.HEADING_WEST;
57-
default -> throw new IllegalArgumentException();
58-
},
59-
vector.value,
60-
false,
61-
false
62-
);
63-
}
64-
6549
private enum NavAction {
6650
NORTH, SOUTH, EAST, WEST, LEFT, RIGHT, FORWARD;
6751

@@ -110,6 +94,17 @@ private static record DirNavInstr(NavAction action, int value) implements NavIns
11094
public @NotNull Point2D accept(@NotNull Point2D ferry, @NotNull NavInstrVisitor visitor) {
11195
return visitor.visit(ferry, this);
11296
}
97+
98+
@GridUtil.GridHeading
99+
public int heading() {
100+
return switch (action) {
101+
case NORTH -> GridUtil.HEADING_NORTH;
102+
case SOUTH -> GridUtil.HEADING_SOUTH;
103+
case EAST -> GridUtil.HEADING_EAST;
104+
case WEST -> GridUtil.HEADING_WEST;
105+
default -> throw new IllegalArgumentException();
106+
};
107+
}
113108
}
114109

115110
private static record ForwardNavInstr(int value) implements NavInstr {

year2020/src/test/java/dev/linl33/adventofcode/year2020/test/Day12Test.java

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
import java.util.Map;
88

9-
import static org.junit.jupiter.api.Assertions.*;
10-
119
class Day12Test implements AdventSolutionTest<Integer, Integer> {
1210

1311
@Override

0 commit comments

Comments
 (0)