Skip to content

Commit ce2f63e

Browse files
committed
refactor: add another minor refactor to day 4
1 parent 938af4a commit ce2f63e

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/main/java/com/adventofcode/flashk/day04/CeresSearch.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ public class CeresSearch {
1313
private static final char A = 'A';
1414
private static final char S = 'S';
1515

16+
private static final char[] word = { M, A, S };
17+
18+
private static final Vector2[] directions = {
19+
Vector2.down(), Vector2.up(), Vector2.right(), Vector2.left(),
20+
Vector2.upLeft(), Vector2.upRight(), Vector2.downLeft(), Vector2.downRight()
21+
};
22+
1623
private final char[][] input;
1724
private final int rows;
1825
private final int cols;
@@ -44,15 +51,11 @@ public long solveB() {
4451
}
4552

4653
private int find(Vector2 xPos) {
47-
char[] word = { M, A, S };
48-
49-
return Stream.of(Vector2.down(), Vector2.up(), Vector2.right(), Vector2.left(),
50-
Vector2.upLeft(), Vector2.upRight(), Vector2.downLeft(), Vector2.downRight())
51-
.mapToInt(dir -> find(word, 0, xPos, dir)).sum();
54+
return Stream.of(directions).mapToInt(dir -> find(0, xPos, dir)).sum();
5255

5356
}
5457

55-
private int find(final char[] word, int letterIndex, Vector2 position, final Vector2 direction) {
58+
private int find(int letterIndex, Vector2 position, final Vector2 direction) {
5659
Vector2 newPos = Vector2.transform(position, direction);
5760

5861
if(isOutOfRange(newPos.getY(), newPos.getX())) {
@@ -63,11 +66,11 @@ private int find(final char[] word, int letterIndex, Vector2 position, final Vec
6366
return 0;
6467
}
6568

66-
if(input[newPos.getY()][newPos.getX()] == S && letterIndex == word.length-1) {
69+
if(letterIndex == word.length-1) {
6770
return 1;
6871
}
6972

70-
return find(word, letterIndex+1, newPos, direction);
73+
return find(letterIndex+1, newPos, direction);
7174
}
7275

7376
private boolean hasMasDiagonal1(Vector2 aPos){

0 commit comments

Comments
 (0)