Skip to content

Commit

Permalink
refactor: refactor day 4 and create an Array2DUtil class
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 4, 2024
1 parent 7d2a840 commit e5f1d12
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 62 deletions.
23 changes: 23 additions & 0 deletions src/main/java/com/adventofcode/flashk/common/Array2DUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.adventofcode.flashk.common;

public class Array2DUtil {

private Array2DUtil() {}

/**
* Transposes the given 2D array.
* @param array the array to transpose.
* @return a new 2D array that is the transpose of the given array.
*/
public static char[][] transpose(char[][] array) {
int rows = array.length;
int cols = array[0].length;
char[][] transposedArray = new char[cols][rows];
for(int row = 0; row < rows; row++) {
for(int col = 0; col < cols; col++) {
transposedArray[col][row] = array[row][col];
}
}
return transposedArray;
}
}
105 changes: 43 additions & 62 deletions src/main/java/com/adventofcode/flashk/day04/CeresSearch.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package com.adventofcode.flashk.day04;

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;

public class CeresSearch {

private static final String XMAS = "XMAS";
private static final String SAMX = "SAMX";
private static final char X = 'X';
private static final char M = 'M';
private static final char A = 'A';
private static final char S = 'S';

private final char[][] input;
private final int rows;
private final int cols;
Expand All @@ -21,117 +31,97 @@ public CeresSearch(char[][] input){

for(int row = 0; row < rows; row++) {
for(int col = 0; col < cols; col++) {
if(input[row][col] == 'X') {
if(input[row][col] == X) {
xPositions.add(new Vector2(col,row));
} else if(input[row][col] == 'A') {
} else if(input[row][col] == A) {
aPositions.add(new Vector2(col,row));
}
}
}
}

public long solveA() {
long wordCount = 0;

wordCount += countHorizontal();
long wordCount = countHorizontal();
wordCount += countVertical();
wordCount += countDiagonal();

return wordCount;
}

public long solveB() {
long wordCount = 0;

for(Vector2 aPosition : aPositions) {
if(countXShapeDiagonal1(aPosition.getY(), aPosition.getX()) && countXShapeDiagonal2(aPosition.getY(), aPosition.getX())) {
wordCount++;
}

}

return wordCount;
return aPositions.stream().filter(this::hasMasDiagonal1).filter(this::hasMasDiagonal2).count();
}


private boolean countXShapeDiagonal1(int row, int col){
private boolean hasMasDiagonal1(Vector2 aPos){

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

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

int downRightRow = row+1;
int downRightCol = col+1;
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'){
if(input[upLeftRow][upLeftCol] == M && input[downRightRow][downRightCol] == S){
return true;
}

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

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

private boolean countXShapeDiagonal2(int row, int col){
private boolean hasMasDiagonal2(Vector2 aPos){

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

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

int downLeftRow = row+1;
int downLeftCol = col-1;
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'){
if(input[upRightRow][upRightCol] == M && input[downLeftRow][downLeftCol] == S){
return true;
}

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

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

private int countHorizontal() {
int totalCount = 0;
for(int row = 0; row < rows; row++) {
String line = new String(input[row]);
totalCount += StringUtils.countMatches(line, "XMAS") + StringUtils.countMatches(line, "SAMX");
totalCount += StringUtils.countMatches(line, XMAS) + StringUtils.countMatches(line, SAMX);
}
return totalCount;
}

private int countVertical() {

char[][] transposedInput = transpose();
char[][] transposedInput = Array2DUtil.transpose(input);
int totalCount = 0;
for(int row = 0; row < rows; row++) {
String line = new String(transposedInput[row]);
totalCount += StringUtils.countMatches(line, "XMAS") + StringUtils.countMatches(line, "SAMX");
totalCount += StringUtils.countMatches(line, XMAS) + StringUtils.countMatches(line, SAMX);
}
return totalCount;
}

private long countDiagonal() {
int totalCount = 0;
for(Vector2 xPosition : xPositions) {
// Recorrer hacia cada una de las 4 esquinas buscando "MAS"
totalCount += upLeftDiagonal(xPosition.getY(), xPosition.getX());
totalCount += upRightDiagonal(xPosition.getY(), xPosition.getX());
totalCount += downLeftDiagonal(xPosition.getY(), xPosition.getX());
Expand All @@ -151,23 +141,23 @@ private int upLeftDiagonal(int row, int col) {
return 0;
}

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

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

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

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

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

Expand All @@ -183,23 +173,23 @@ private int upRightDiagonal(int row, int col) {
return 0;
}

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

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

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

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

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

Expand All @@ -214,23 +204,23 @@ private int downLeftDiagonal(int row, int col) {
return 0;
}

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

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

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

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

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

Expand All @@ -245,23 +235,23 @@ private int downRightDiagonal(int row, int col) {
return 0;
}

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

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

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

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

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

Expand All @@ -272,13 +262,4 @@ private boolean isOutOfRange(int row, int col) {
return row < 0 || row >= rows || col < 0 || col >= cols;
}

private char[][] transpose() {
char[][] transposedInput = new char[cols][rows];
for(int row = 0; row < rows; row++) {
for(int col = 0; col < cols; col++) {
transposedInput[col][row] = input[row][col];
}
}
return transposedInput;
}
}

0 comments on commit e5f1d12

Please sign in to comment.