Skip to content

Commit

Permalink
refactor: refactor day 4 part 1 to use recursivity
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 4, 2024
1 parent e5f1d12 commit 64f0faa
Showing 1 changed file with 45 additions and 154 deletions.
199 changes: 45 additions & 154 deletions src/main/java/com/adventofcode/flashk/day04/CeresSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.adventofcode.flashk.common.Array2DUtil;
import com.adventofcode.flashk.common.Vector2;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.jgrapht.util.ArrayUtil;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -13,6 +11,7 @@ public class CeresSearch {

private static final String XMAS = "XMAS";
private static final String SAMX = "SAMX";
private static final String MAS = "MAS";
private static final char X = 'X';
private static final char M = 'M';
private static final char A = 'A';
Expand Down Expand Up @@ -41,62 +40,14 @@ public CeresSearch(char[][] input){
}

public long solveA() {
long wordCount = countHorizontal();
wordCount += countVertical();
wordCount += countDiagonal();

return wordCount;
long totalCount = countHorizontal();
totalCount += countVertical();
totalCount += countDiagonal();
return totalCount;
}

public long solveB() {
return aPositions.stream().filter(this::hasMasDiagonal1).filter(this::hasMasDiagonal2).count();
}


private boolean hasMasDiagonal1(Vector2 aPos){

int upLeftRow = aPos.getY()-1;
int upLeftCol = aPos.getX()-1;

if(isOutOfRange(upLeftRow, upLeftCol)) {
return false;
}

int downRightRow = aPos.getY()+1;
int downRightCol = aPos.getX()+1;

if(isOutOfRange(downRightRow, downRightCol)) {
return false;
}

if(input[upLeftRow][upLeftCol] == M && input[downRightRow][downRightCol] == S){
return true;
}

return input[upLeftRow][upLeftCol] == S && input[downRightRow][downRightCol] == M;
}

private boolean hasMasDiagonal2(Vector2 aPos){

int upRightRow = aPos.getY()-1;
int upRightCol = aPos.getX()+1;

if(isOutOfRange(upRightRow, upRightCol)) {
return false;
}

int downLeftRow = aPos.getY()+1;
int downLeftCol = aPos.getX()-1;

if(isOutOfRange(downLeftRow, downLeftCol)) {
return false;
}

if(input[upRightRow][upRightCol] == M && input[downLeftRow][downLeftCol] == S){
return true;
}

return input[upRightRow][upRightCol] == S && input[downLeftRow][downLeftCol] == M;
return aPositions.stream().filter(this::hasMasDiagonal1).filter(this::hasMasDiagonal2).count();
}

private int countHorizontal() {
Expand All @@ -120,142 +71,82 @@ private int countVertical() {
}

private long countDiagonal() {
int totalCount = 0;
for(Vector2 xPosition : xPositions) {
totalCount += upLeftDiagonal(xPosition.getY(), xPosition.getX());
totalCount += upRightDiagonal(xPosition.getY(), xPosition.getX());
totalCount += downLeftDiagonal(xPosition.getY(), xPosition.getX());
totalCount += downRightDiagonal(xPosition.getY(), xPosition.getX());
}

return totalCount;
return xPositions.stream().map(this::find).reduce(0, Integer::sum);
}

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

private int upLeftDiagonal(int row, int col) {

int nextPosRow = row-1;
int nextPosCol = col-1;

if(isOutOfRange(nextPosRow, nextPosCol)) {
return 0;
}

if(input[nextPosRow][nextPosCol] != M) {
return 0;
}

if(isOutOfRange(--nextPosRow, --nextPosCol)) {
return 0;
}
int totalCount = find(word, 0, xPos, new Vector2(-1,-1));
totalCount += find(word, 0, xPos, new Vector2(-1,1));
totalCount += find(word, 0, xPos, new Vector2(1,-1));
totalCount += find(word, 0, xPos, new Vector2(1,1));

if(input[nextPosRow][nextPosCol] != A) {
return 0;
}

if(isOutOfRange(--nextPosRow, --nextPosCol)) {
return 0;
}

if(input[nextPosRow][nextPosCol] != S) {
return 0;
}

return 1;
return totalCount;
}

private int upRightDiagonal(int row, int col) {

int nextPosRow = row-1;
int nextPosCol = col+1;

if(isOutOfRange(nextPosRow, nextPosCol)) {
return 0;
}

if(input[nextPosRow][nextPosCol] != M) {
return 0;
}

if(isOutOfRange(--nextPosRow, ++nextPosCol)) {
return 0;
}
private int find(final char[] word, int letterIndex, Vector2 position, final Vector2 direction) {
Vector2 newPos = Vector2.transform(position, direction);

if(input[nextPosRow][nextPosCol] != A) {
if(isOutOfRange(newPos.getY(), newPos.getX())) {
return 0;
}

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

if(input[nextPosRow][nextPosCol] != S) {
return 0;
if(input[newPos.getY()][newPos.getX()] == S && letterIndex == word.length-1) {
return 1;
}

return 1;
return find(word, letterIndex+1, newPos, direction);
}

private int downLeftDiagonal(int row, int col) {
int nextPosRow = row+1;
int nextPosCol = col-1;

if(isOutOfRange(nextPosRow, nextPosCol)) {
return 0;
}
private boolean hasMasDiagonal1(Vector2 aPos){

if(input[nextPosRow][nextPosCol] != M) {
return 0;
}
int upLeftRow = aPos.getY()-1;
int upLeftCol = aPos.getX()-1;

if(isOutOfRange(++nextPosRow, --nextPosCol)) {
return 0;
if(isOutOfRange(upLeftRow, upLeftCol)) {
return false;
}

if(input[nextPosRow][nextPosCol] != A) {
return 0;
}
int downRightRow = aPos.getY()+1;
int downRightCol = aPos.getX()+1;

if(isOutOfRange(++nextPosRow, --nextPosCol)) {
return 0;
if(isOutOfRange(downRightRow, downRightCol)) {
return false;
}

if(input[nextPosRow][nextPosCol] != S) {
return 0;
if(input[upLeftRow][upLeftCol] == M && input[downRightRow][downRightCol] == S){
return true;
}

return 1;
return input[upLeftRow][upLeftCol] == S && input[downRightRow][downRightCol] == M;
}

private int downRightDiagonal(int row, int col) {
int nextPosRow = row+1;
int nextPosCol = col+1;

if(isOutOfRange(nextPosRow, nextPosCol)) {
return 0;
}
private boolean hasMasDiagonal2(Vector2 aPos){

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

if(isOutOfRange(++nextPosRow, ++nextPosCol)) {
return 0;
if(isOutOfRange(upRightRow, upRightCol)) {
return false;
}

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

if(isOutOfRange(++nextPosRow, ++nextPosCol)) {
return 0;
if(isOutOfRange(downLeftRow, downLeftCol)) {
return false;
}

if(input[nextPosRow][nextPosCol] != S) {
return 0;
if(input[upRightRow][upRightCol] == M && input[downLeftRow][downLeftCol] == S){
return true;
}

return 1;
return input[upRightRow][upRightCol] == S && input[downLeftRow][downLeftCol] == M;
}

private boolean isOutOfRange(int row, int col) {
Expand Down

0 comments on commit 64f0faa

Please sign in to comment.