Skip to content

Commit 64f0faa

Browse files
committed
refactor: refactor day 4 part 1 to use recursivity
1 parent e5f1d12 commit 64f0faa

File tree

1 file changed

+45
-154
lines changed

1 file changed

+45
-154
lines changed

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

Lines changed: 45 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import com.adventofcode.flashk.common.Array2DUtil;
44
import com.adventofcode.flashk.common.Vector2;
5-
import org.apache.commons.lang3.ArrayUtils;
65
import org.apache.commons.lang3.StringUtils;
7-
import org.jgrapht.util.ArrayUtil;
86

97
import java.util.ArrayList;
108
import java.util.List;
@@ -13,6 +11,7 @@ public class CeresSearch {
1311

1412
private static final String XMAS = "XMAS";
1513
private static final String SAMX = "SAMX";
14+
private static final String MAS = "MAS";
1615
private static final char X = 'X';
1716
private static final char M = 'M';
1817
private static final char A = 'A';
@@ -41,62 +40,14 @@ public CeresSearch(char[][] input){
4140
}
4241

4342
public long solveA() {
44-
long wordCount = countHorizontal();
45-
wordCount += countVertical();
46-
wordCount += countDiagonal();
47-
48-
return wordCount;
43+
long totalCount = countHorizontal();
44+
totalCount += countVertical();
45+
totalCount += countDiagonal();
46+
return totalCount;
4947
}
5048

5149
public long solveB() {
52-
return aPositions.stream().filter(this::hasMasDiagonal1).filter(this::hasMasDiagonal2).count();
53-
}
54-
55-
56-
private boolean hasMasDiagonal1(Vector2 aPos){
57-
58-
int upLeftRow = aPos.getY()-1;
59-
int upLeftCol = aPos.getX()-1;
60-
61-
if(isOutOfRange(upLeftRow, upLeftCol)) {
62-
return false;
63-
}
64-
65-
int downRightRow = aPos.getY()+1;
66-
int downRightCol = aPos.getX()+1;
67-
68-
if(isOutOfRange(downRightRow, downRightCol)) {
69-
return false;
70-
}
71-
72-
if(input[upLeftRow][upLeftCol] == M && input[downRightRow][downRightCol] == S){
73-
return true;
74-
}
75-
76-
return input[upLeftRow][upLeftCol] == S && input[downRightRow][downRightCol] == M;
77-
}
78-
79-
private boolean hasMasDiagonal2(Vector2 aPos){
80-
81-
int upRightRow = aPos.getY()-1;
82-
int upRightCol = aPos.getX()+1;
83-
84-
if(isOutOfRange(upRightRow, upRightCol)) {
85-
return false;
86-
}
87-
88-
int downLeftRow = aPos.getY()+1;
89-
int downLeftCol = aPos.getX()-1;
90-
91-
if(isOutOfRange(downLeftRow, downLeftCol)) {
92-
return false;
93-
}
94-
95-
if(input[upRightRow][upRightCol] == M && input[downLeftRow][downLeftCol] == S){
96-
return true;
97-
}
98-
99-
return input[upRightRow][upRightCol] == S && input[downLeftRow][downLeftCol] == M;
50+
return aPositions.stream().filter(this::hasMasDiagonal1).filter(this::hasMasDiagonal2).count();
10051
}
10152

10253
private int countHorizontal() {
@@ -120,142 +71,82 @@ private int countVertical() {
12071
}
12172

12273
private long countDiagonal() {
123-
int totalCount = 0;
124-
for(Vector2 xPosition : xPositions) {
125-
totalCount += upLeftDiagonal(xPosition.getY(), xPosition.getX());
126-
totalCount += upRightDiagonal(xPosition.getY(), xPosition.getX());
127-
totalCount += downLeftDiagonal(xPosition.getY(), xPosition.getX());
128-
totalCount += downRightDiagonal(xPosition.getY(), xPosition.getX());
129-
}
130-
131-
return totalCount;
74+
return xPositions.stream().map(this::find).reduce(0, Integer::sum);
13275
}
13376

77+
private int find(Vector2 xPos) {
78+
char[] word = MAS.toCharArray();
13479

135-
private int upLeftDiagonal(int row, int col) {
136-
137-
int nextPosRow = row-1;
138-
int nextPosCol = col-1;
139-
140-
if(isOutOfRange(nextPosRow, nextPosCol)) {
141-
return 0;
142-
}
143-
144-
if(input[nextPosRow][nextPosCol] != M) {
145-
return 0;
146-
}
147-
148-
if(isOutOfRange(--nextPosRow, --nextPosCol)) {
149-
return 0;
150-
}
80+
int totalCount = find(word, 0, xPos, new Vector2(-1,-1));
81+
totalCount += find(word, 0, xPos, new Vector2(-1,1));
82+
totalCount += find(word, 0, xPos, new Vector2(1,-1));
83+
totalCount += find(word, 0, xPos, new Vector2(1,1));
15184

152-
if(input[nextPosRow][nextPosCol] != A) {
153-
return 0;
154-
}
155-
156-
if(isOutOfRange(--nextPosRow, --nextPosCol)) {
157-
return 0;
158-
}
159-
160-
if(input[nextPosRow][nextPosCol] != S) {
161-
return 0;
162-
}
163-
164-
return 1;
85+
return totalCount;
16586
}
16687

167-
private int upRightDiagonal(int row, int col) {
168-
169-
int nextPosRow = row-1;
170-
int nextPosCol = col+1;
171-
172-
if(isOutOfRange(nextPosRow, nextPosCol)) {
173-
return 0;
174-
}
175-
176-
if(input[nextPosRow][nextPosCol] != M) {
177-
return 0;
178-
}
179-
180-
if(isOutOfRange(--nextPosRow, ++nextPosCol)) {
181-
return 0;
182-
}
88+
private int find(final char[] word, int letterIndex, Vector2 position, final Vector2 direction) {
89+
Vector2 newPos = Vector2.transform(position, direction);
18390

184-
if(input[nextPosRow][nextPosCol] != A) {
91+
if(isOutOfRange(newPos.getY(), newPos.getX())) {
18592
return 0;
18693
}
18794

188-
if(isOutOfRange(--nextPosRow, ++nextPosCol)) {
95+
if(input[newPos.getY()][newPos.getX()] != word[letterIndex]) {
18996
return 0;
19097
}
19198

192-
if(input[nextPosRow][nextPosCol] != S) {
193-
return 0;
99+
if(input[newPos.getY()][newPos.getX()] == S && letterIndex == word.length-1) {
100+
return 1;
194101
}
195102

196-
return 1;
103+
return find(word, letterIndex+1, newPos, direction);
197104
}
198105

199-
private int downLeftDiagonal(int row, int col) {
200-
int nextPosRow = row+1;
201-
int nextPosCol = col-1;
202-
203-
if(isOutOfRange(nextPosRow, nextPosCol)) {
204-
return 0;
205-
}
106+
private boolean hasMasDiagonal1(Vector2 aPos){
206107

207-
if(input[nextPosRow][nextPosCol] != M) {
208-
return 0;
209-
}
108+
int upLeftRow = aPos.getY()-1;
109+
int upLeftCol = aPos.getX()-1;
210110

211-
if(isOutOfRange(++nextPosRow, --nextPosCol)) {
212-
return 0;
111+
if(isOutOfRange(upLeftRow, upLeftCol)) {
112+
return false;
213113
}
214114

215-
if(input[nextPosRow][nextPosCol] != A) {
216-
return 0;
217-
}
115+
int downRightRow = aPos.getY()+1;
116+
int downRightCol = aPos.getX()+1;
218117

219-
if(isOutOfRange(++nextPosRow, --nextPosCol)) {
220-
return 0;
118+
if(isOutOfRange(downRightRow, downRightCol)) {
119+
return false;
221120
}
222121

223-
if(input[nextPosRow][nextPosCol] != S) {
224-
return 0;
122+
if(input[upLeftRow][upLeftCol] == M && input[downRightRow][downRightCol] == S){
123+
return true;
225124
}
226125

227-
return 1;
126+
return input[upLeftRow][upLeftCol] == S && input[downRightRow][downRightCol] == M;
228127
}
229128

230-
private int downRightDiagonal(int row, int col) {
231-
int nextPosRow = row+1;
232-
int nextPosCol = col+1;
233-
234-
if(isOutOfRange(nextPosRow, nextPosCol)) {
235-
return 0;
236-
}
129+
private boolean hasMasDiagonal2(Vector2 aPos){
237130

238-
if(input[nextPosRow][nextPosCol] != M) {
239-
return 0;
240-
}
131+
int upRightRow = aPos.getY()-1;
132+
int upRightCol = aPos.getX()+1;
241133

242-
if(isOutOfRange(++nextPosRow, ++nextPosCol)) {
243-
return 0;
134+
if(isOutOfRange(upRightRow, upRightCol)) {
135+
return false;
244136
}
245137

246-
if(input[nextPosRow][nextPosCol] != A) {
247-
return 0;
248-
}
138+
int downLeftRow = aPos.getY()+1;
139+
int downLeftCol = aPos.getX()-1;
249140

250-
if(isOutOfRange(++nextPosRow, ++nextPosCol)) {
251-
return 0;
141+
if(isOutOfRange(downLeftRow, downLeftCol)) {
142+
return false;
252143
}
253144

254-
if(input[nextPosRow][nextPosCol] != S) {
255-
return 0;
145+
if(input[upRightRow][upRightCol] == M && input[downLeftRow][downLeftCol] == S){
146+
return true;
256147
}
257148

258-
return 1;
149+
return input[upRightRow][upRightCol] == S && input[downLeftRow][downLeftCol] == M;
259150
}
260151

261152
private boolean isOutOfRange(int row, int col) {

0 commit comments

Comments
 (0)