Complete day 4
This commit is contained in:
@@ -13,12 +13,14 @@ import com.mattrixwv.adventOfCode24.days.Problem3;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem4;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem5;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem6;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem7;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem8;
|
||||
|
||||
|
||||
public class ProblemSelector{
|
||||
private static final Scanner input = new Scanner(System.in);
|
||||
//Holds the valid problem numbers
|
||||
protected static final List<Integer> PROBLEM_NUMBERS = List.of(0, 1, 2, 3, 4, 5, 6);
|
||||
protected static final List<Integer> PROBLEM_NUMBERS = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8);
|
||||
|
||||
|
||||
private ProblemSelector(){
|
||||
@@ -36,6 +38,8 @@ public class ProblemSelector{
|
||||
case 4 : day = new Problem4(); break;
|
||||
case 5 : day = new Problem5(); break;
|
||||
case 6 : day = new Problem6(); break;
|
||||
case 7 : day = new Problem7(); break;
|
||||
case 8 : day = new Problem8(); break;
|
||||
default: throw new InvalidParameterException();
|
||||
}
|
||||
return day;
|
||||
|
||||
194
src/main/java/com/mattrixwv/adventOfCode24/days/Problem7.java
Normal file
194
src/main/java/com/mattrixwv/adventOfCode24/days/Problem7.java
Normal file
@@ -0,0 +1,194 @@
|
||||
package com.mattrixwv.adventOfCode24.days;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import com.mattrixwv.Stopwatch;
|
||||
|
||||
|
||||
public class Problem7 extends Problem{
|
||||
private static final String inputFileName = "days/Problem7.txt";
|
||||
private List<String> wordSearchGrid;
|
||||
|
||||
|
||||
public Problem7(){
|
||||
super();
|
||||
description = "Find how many times the word XMAS appears in the grid.";
|
||||
result = "Unresolved";
|
||||
}
|
||||
|
||||
public String runSolution(){
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.start();
|
||||
|
||||
wordSearchGrid = new ArrayList<>();
|
||||
int xmasCount = 0;
|
||||
|
||||
|
||||
//Read the file
|
||||
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||
scanner.useDelimiter("\n");
|
||||
while(scanner.hasNext()){
|
||||
wordSearchGrid.add(scanner.next());
|
||||
}
|
||||
}
|
||||
|
||||
//Step through each character and find any instances of the word XMAS
|
||||
for(int rowNum = 0;rowNum < wordSearchGrid.size();++rowNum){
|
||||
String row = wordSearchGrid.get(rowNum);
|
||||
for(int colNum = 0;colNum < row.length();++colNum){
|
||||
if(row.charAt(colNum) == 'X'){
|
||||
int rookXmas = getNumRookXmas(rowNum, colNum);
|
||||
int bishopXmas = getNumBishopXmas(rowNum, colNum);
|
||||
xmasCount += rookXmas + bishopXmas;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Save the results
|
||||
timer.stop();
|
||||
result = "The word XMAS appears " + xmasCount + " times in the grid.\nIt took " + timer.toString() + " to run the algorithm.";
|
||||
return result;
|
||||
}
|
||||
|
||||
private int getNumRookXmas(int rowNum, int colNum){
|
||||
int occurrences = 0;
|
||||
if(checkForwards(rowNum, colNum)){
|
||||
++occurrences;
|
||||
}
|
||||
if(checkBackwards(rowNum, colNum)){
|
||||
++occurrences;
|
||||
}
|
||||
if(checkUp(rowNum, colNum)){
|
||||
++occurrences;
|
||||
}
|
||||
if(checkDown(rowNum, colNum)){
|
||||
++occurrences;
|
||||
}
|
||||
return occurrences;
|
||||
}
|
||||
|
||||
private int getNumBishopXmas(int rowNum, int colNum){
|
||||
int occurrences = 0;
|
||||
if(checkForwardsUp(rowNum, colNum)){
|
||||
++occurrences;
|
||||
}
|
||||
if(checkForwardsDown(rowNum, colNum)){
|
||||
++occurrences;
|
||||
}
|
||||
if(checkBackwardsUp(rowNum, colNum)){
|
||||
++occurrences;
|
||||
}
|
||||
if(checkBackwardsDown(rowNum, colNum)){
|
||||
++occurrences;
|
||||
}
|
||||
return occurrences;
|
||||
}
|
||||
|
||||
|
||||
private boolean checkForwards(int rowNum, int colNum){
|
||||
return
|
||||
//Check that the bounds are valid
|
||||
(rowNum >= 0) && (rowNum < wordSearchGrid.size()) &&
|
||||
(colNum >= 0) && (colNum + 3 < wordSearchGrid.get(rowNum).length()) &&
|
||||
//Check that the word XMAS appears
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum) == 'X') &&
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum + 1) == 'M') &&
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum + 2) == 'A') &&
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum + 3) == 'S');
|
||||
}
|
||||
|
||||
private boolean checkBackwards(int rowNum, int colNum){
|
||||
return
|
||||
//Check that the bounds are valid
|
||||
(rowNum >= 0) && (rowNum < wordSearchGrid.size()) &&
|
||||
(colNum >= 3) && (colNum < wordSearchGrid.get(rowNum).length()) &&
|
||||
//Check that the word XMAS appears
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum) == 'X') &&
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum - 1) == 'M') &&
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum - 2) == 'A') &&
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum - 3) == 'S');
|
||||
}
|
||||
|
||||
private boolean checkUp(int rowNum, int colNum){
|
||||
return
|
||||
//Check that the bounds are valid
|
||||
(rowNum >= 3) && (rowNum < wordSearchGrid.size()) &&
|
||||
(colNum >= 0) && (colNum < wordSearchGrid.get(rowNum).length()) && (colNum < wordSearchGrid.get(rowNum - 1).length()) && (colNum < wordSearchGrid.get(rowNum - 2).length()) && (colNum < wordSearchGrid.get(rowNum - 3).length()) &&
|
||||
//Check that the word XMAS appears
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum) == 'X') &&
|
||||
(wordSearchGrid.get(rowNum - 1).charAt(colNum) == 'M') &&
|
||||
(wordSearchGrid.get(rowNum - 2).charAt(colNum) == 'A') &&
|
||||
(wordSearchGrid.get(rowNum - 3).charAt(colNum) == 'S');
|
||||
}
|
||||
|
||||
private boolean checkDown(int rowNum, int colNum){
|
||||
return
|
||||
//Check that the bounds are valid
|
||||
(rowNum >= 0) && (rowNum + 3 < wordSearchGrid.size()) &&
|
||||
(colNum >= 0) && (colNum < wordSearchGrid.get(rowNum).length()) && (colNum < wordSearchGrid.get(rowNum + 1).length()) && (colNum < wordSearchGrid.get(rowNum + 2).length()) && (colNum < wordSearchGrid.get(rowNum + 3).length()) &&
|
||||
//Check that the word XMAS appears
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum) == 'X') &&
|
||||
(wordSearchGrid.get(rowNum + 1).charAt(colNum) == 'M') &&
|
||||
(wordSearchGrid.get(rowNum + 2).charAt(colNum) == 'A') &&
|
||||
(wordSearchGrid.get(rowNum + 3).charAt(colNum) == 'S');
|
||||
}
|
||||
|
||||
private boolean checkForwardsUp(int rowNum, int colNum){
|
||||
return
|
||||
//Check that the bounds are valid
|
||||
(rowNum >= 3) && (rowNum < wordSearchGrid.size()) &&
|
||||
(colNum >= 0) && (colNum < wordSearchGrid.get(rowNum).length()) && (colNum + 1 < wordSearchGrid.get(rowNum - 1).length()) && (colNum + 2 < wordSearchGrid.get(rowNum - 2).length()) && (colNum + 3 < wordSearchGrid.get(rowNum - 3).length()) &&
|
||||
//Check that the word XMAS appears
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum) == 'X') &&
|
||||
(wordSearchGrid.get(rowNum - 1).charAt(colNum + 1) == 'M') &&
|
||||
(wordSearchGrid.get(rowNum - 2).charAt(colNum + 2) == 'A') &&
|
||||
(wordSearchGrid.get(rowNum - 3).charAt(colNum + 3) == 'S');
|
||||
}
|
||||
|
||||
private boolean checkForwardsDown(int rowNum, int colNum){
|
||||
return
|
||||
//Check that the bounds are valid
|
||||
(rowNum >= 0) && (rowNum + 3 < wordSearchGrid.size()) &&
|
||||
(colNum >= 0) && (colNum < wordSearchGrid.get(rowNum).length()) && (colNum + 1 < wordSearchGrid.get(rowNum + 1).length()) && (colNum + 2 < wordSearchGrid.get(rowNum + 2).length()) && (colNum + 3 < wordSearchGrid.get(rowNum + 3).length()) &&
|
||||
//Check that the word XMAS appears
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum) == 'X') &&
|
||||
(wordSearchGrid.get(rowNum + 1).charAt(colNum + 1) == 'M') &&
|
||||
(wordSearchGrid.get(rowNum + 2).charAt(colNum + 2) == 'A') &&
|
||||
(wordSearchGrid.get(rowNum + 3).charAt(colNum + 3) == 'S');
|
||||
}
|
||||
|
||||
private boolean checkBackwardsUp(int rowNum, int colNum){
|
||||
return
|
||||
//Check that the bounds are valid
|
||||
(rowNum >= 3) && (rowNum < wordSearchGrid.size()) &&
|
||||
(colNum >= 3) && (colNum < wordSearchGrid.get(rowNum).length()) && (colNum - 1 < wordSearchGrid.get(rowNum - 1).length()) &&(colNum - 2 < wordSearchGrid.get(rowNum - 2).length()) &&(colNum - 3 < wordSearchGrid.get(rowNum - 3).length()) &&
|
||||
//Check that the word XMAS appears
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum) == 'X') &&
|
||||
(wordSearchGrid.get(rowNum - 1).charAt(colNum - 1) == 'M') &&
|
||||
(wordSearchGrid.get(rowNum - 2).charAt(colNum - 2) == 'A') &&
|
||||
(wordSearchGrid.get(rowNum - 3).charAt(colNum - 3) == 'S');
|
||||
}
|
||||
|
||||
private boolean checkBackwardsDown(int rowNum, int colNum){
|
||||
return
|
||||
//Check that the bounds are valid
|
||||
(rowNum >= 0) && (rowNum + 3 < wordSearchGrid.size()) &&
|
||||
(colNum >= 3) && (colNum < wordSearchGrid.get(rowNum).length()) && (colNum - 1 < wordSearchGrid.get(rowNum + 1).length()) &&(colNum - 2 < wordSearchGrid.get(rowNum + 2).length()) &&(colNum - 3 < wordSearchGrid.get(rowNum + 3).length()) &&
|
||||
//Check that the word XMAS appears
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum) == 'X') &&
|
||||
(wordSearchGrid.get(rowNum + 1).charAt(colNum - 1) == 'M') &&
|
||||
(wordSearchGrid.get(rowNum + 2).charAt(colNum - 2) == 'A') &&
|
||||
(wordSearchGrid.get(rowNum + 3).charAt(colNum - 3) == 'S');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Find how many times the word XMAS appears in the grid.
|
||||
The word XMAS appears 2633 times in the grid.
|
||||
It took 15.871 milliseconds to run the algorithm.
|
||||
*/
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.mattrixwv.adventOfCode24.days;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import com.mattrixwv.Stopwatch;
|
||||
|
||||
|
||||
public class Problem8 extends Problem{
|
||||
private static final String inputFileName = "days/Problem7.txt";
|
||||
private List<String> wordSearchGrid;
|
||||
|
||||
|
||||
public Problem8(){
|
||||
super();
|
||||
description = "Find how many times the word MAS appears in an X pattern in the grid.";
|
||||
result = "Unresolved";
|
||||
}
|
||||
|
||||
public String runSolution(){
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.start();
|
||||
|
||||
wordSearchGrid = new ArrayList<>();
|
||||
int xmasCount = 0;
|
||||
|
||||
|
||||
//Read the file
|
||||
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||
scanner.useDelimiter("\n");
|
||||
while(scanner.hasNext()){
|
||||
wordSearchGrid.add(scanner.next());
|
||||
}
|
||||
}
|
||||
//Step through each character and find any instances of the word MAS in an X pattern
|
||||
for(int rowNum = 1;rowNum < wordSearchGrid.size() - 1;++rowNum){
|
||||
String row = wordSearchGrid.get(rowNum);
|
||||
for(int colNum = 1;colNum < row.length();++colNum){
|
||||
if(row.charAt(colNum) == 'A'){
|
||||
if(isTLBRDiagonal(rowNum, colNum) && isBLTRDiagonal(rowNum, colNum)){
|
||||
++xmasCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Save the results
|
||||
timer.stop();
|
||||
result = "The word MAS appears in an X pattern " + xmasCount + " times in the grid.\nIt took " + timer.toString() + " to run the algorithm.";
|
||||
return result;
|
||||
}
|
||||
|
||||
private boolean isTLBRDiagonal(int rowNum, int colNum){
|
||||
return
|
||||
//Check that the bounds are valid
|
||||
(rowNum >= 1) && (rowNum + 1 < wordSearchGrid.size()) &&
|
||||
(colNum >= 1) && (colNum + 1 < wordSearchGrid.get(rowNum - 1).length()) && (colNum < wordSearchGrid.get(rowNum).length()) && (colNum + 1 < wordSearchGrid.get(rowNum + 1).length()) &&
|
||||
//Check that the word MAS appears top left to bottom right
|
||||
((
|
||||
(wordSearchGrid.get(rowNum - 1).charAt(colNum - 1) == 'M') &&
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum) == 'A') &&
|
||||
(wordSearchGrid.get(rowNum + 1).charAt(colNum + 1) == 'S')
|
||||
) ||
|
||||
//Check that the word MAS appears bottom right to top left
|
||||
(
|
||||
(wordSearchGrid.get(rowNum + 1).charAt(colNum + 1) == 'M') &&
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum) == 'A') &&
|
||||
(wordSearchGrid.get(rowNum - 1).charAt(colNum - 1) == 'S')
|
||||
));
|
||||
}
|
||||
|
||||
private boolean isBLTRDiagonal(int rowNum, int colNum){
|
||||
return
|
||||
//Check that the bounds are valid
|
||||
(rowNum >= 1) && (rowNum + 1 < wordSearchGrid.size()) &&
|
||||
(colNum >= 1) && (colNum + 1 < wordSearchGrid.get(rowNum - 1).length()) && (colNum < wordSearchGrid.get(rowNum).length()) && (colNum + 1 < wordSearchGrid.get(rowNum + 1).length()) &&
|
||||
//Check that the word MAS appears bottom left to top right
|
||||
((
|
||||
(wordSearchGrid.get(rowNum + 1).charAt(colNum - 1) == 'M') &&
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum) == 'A') &&
|
||||
(wordSearchGrid.get(rowNum - 1).charAt(colNum + 1) == 'S')
|
||||
) ||
|
||||
//Check that the word MAS appears top right to bottom left
|
||||
(
|
||||
(wordSearchGrid.get(rowNum - 1).charAt(colNum + 1) == 'M') &&
|
||||
(wordSearchGrid.get(rowNum).charAt(colNum) == 'A') &&
|
||||
(wordSearchGrid.get(rowNum + 1).charAt(colNum - 1) == 'S')
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Find how many times the word MAS appears in an X pattern in the grid.
|
||||
The word MAS appears in an X pattern 1936 times in the grid.
|
||||
It took 6.451 milliseconds to run the algorithm.
|
||||
*/
|
||||
Reference in New Issue
Block a user