Skip to content

Commit 938af4a

Browse files
committed
refactor: refactor day 4 part 1
1 parent 5a24500 commit 938af4a

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.ArrayList;
66
import java.util.List;
7+
import java.util.stream.Stream;
78

89
public class CeresSearch {
910

@@ -35,7 +36,7 @@ public CeresSearch(char[][] input){
3536
}
3637

3738
public long solveA() {
38-
return xPositions.stream().map(this::find).reduce(0, Integer::sum);
39+
return xPositions.stream().mapToInt(this::find).sum();
3940
}
4041

4142
public long solveB() {
@@ -45,16 +46,10 @@ public long solveB() {
4546
private int find(Vector2 xPos) {
4647
char[] word = { M, A, S };
4748

48-
int totalCount = find(word, 0, xPos, new Vector2(-1,-1));
49-
totalCount += find(word, 0, xPos, new Vector2(-1,1));
50-
totalCount += find(word, 0, xPos, new Vector2(1,-1));
51-
totalCount += find(word, 0, xPos, new Vector2(1,1));
52-
totalCount += find(word, 0, xPos, new Vector2(-1,0));
53-
totalCount += find(word, 0, xPos, new Vector2(1,0));
54-
totalCount += find(word, 0, xPos, new Vector2(0,-1));
55-
totalCount += find(word, 0, xPos, new Vector2(0,1));
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();
5652

57-
return totalCount;
5853
}
5954

6055
private int find(final char[] word, int letterIndex, Vector2 position, final Vector2 direction) {

0 commit comments

Comments
 (0)