Complete day 2
This commit is contained in:
@@ -9,12 +9,14 @@ import java.util.StringJoiner;
|
|||||||
import com.mattrixwv.adventOfCode24.days.Problem;
|
import com.mattrixwv.adventOfCode24.days.Problem;
|
||||||
import com.mattrixwv.adventOfCode24.days.Problem1;
|
import com.mattrixwv.adventOfCode24.days.Problem1;
|
||||||
import com.mattrixwv.adventOfCode24.days.Problem2;
|
import com.mattrixwv.adventOfCode24.days.Problem2;
|
||||||
|
import com.mattrixwv.adventOfCode24.days.Problem3;
|
||||||
|
import com.mattrixwv.adventOfCode24.days.Problem4;
|
||||||
|
|
||||||
|
|
||||||
public class ProblemSelector{
|
public class ProblemSelector{
|
||||||
private static final Scanner input = new Scanner(System.in);
|
private static final Scanner input = new Scanner(System.in);
|
||||||
//Holds the valid problem numbers
|
//Holds the valid problem numbers
|
||||||
protected static final List<Integer> PROBLEM_NUMBERS = List.of(0, 1, 2);
|
protected static final List<Integer> PROBLEM_NUMBERS = List.of(0, 1, 2, 3, 4);
|
||||||
|
|
||||||
|
|
||||||
private ProblemSelector(){
|
private ProblemSelector(){
|
||||||
@@ -28,6 +30,8 @@ public class ProblemSelector{
|
|||||||
switch(dayNumber){
|
switch(dayNumber){
|
||||||
case 1 : day = new Problem1(); break;
|
case 1 : day = new Problem1(); break;
|
||||||
case 2 : day = new Problem2(); break;
|
case 2 : day = new Problem2(); break;
|
||||||
|
case 3 : day = new Problem3(); break;
|
||||||
|
case 4 : day = new Problem4(); break;
|
||||||
default: throw new InvalidParameterException();
|
default: throw new InvalidParameterException();
|
||||||
}
|
}
|
||||||
return day;
|
return day;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.mattrixwv.adventOfCode24.days;
|
package com.mattrixwv.adventOfCode24.days;
|
||||||
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -7,6 +8,7 @@ import java.util.Scanner;
|
|||||||
|
|
||||||
import com.mattrixwv.Stopwatch;
|
import com.mattrixwv.Stopwatch;
|
||||||
|
|
||||||
|
|
||||||
public class Problem2 extends Problem{
|
public class Problem2 extends Problem{
|
||||||
private static final String inputFileName = "days/Problem1.txt";
|
private static final String inputFileName = "days/Problem1.txt";
|
||||||
|
|
||||||
|
|||||||
142
src/main/java/com/mattrixwv/adventOfCode24/days/Problem3.java
Normal file
142
src/main/java/com/mattrixwv/adventOfCode24/days/Problem3.java
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
package com.mattrixwv.adventOfCode24.days;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import com.mattrixwv.Stopwatch;
|
||||||
|
|
||||||
|
|
||||||
|
public class Problem3 extends Problem{
|
||||||
|
private static final String inputFileName = "days/Problem3.txt";
|
||||||
|
|
||||||
|
|
||||||
|
public Problem3(){
|
||||||
|
super();
|
||||||
|
description = "Find the \"safe\" reports from the given list.";
|
||||||
|
result = "Unresolved";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String runSolution(){
|
||||||
|
Stopwatch timer = new Stopwatch();
|
||||||
|
timer.start();
|
||||||
|
|
||||||
|
List<String> reports = new ArrayList<>();
|
||||||
|
int safeReports = 0;
|
||||||
|
|
||||||
|
|
||||||
|
//Read elements from the file
|
||||||
|
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||||
|
scanner.useDelimiter("\n");
|
||||||
|
while(scanner.hasNext()){
|
||||||
|
String report = scanner.next();
|
||||||
|
if(!report.isBlank()){
|
||||||
|
reports.add(report);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Count how many reports are safe
|
||||||
|
for(String report : reports){
|
||||||
|
if(isSafeReport(convertReportToArray(report), false)){
|
||||||
|
++safeReports;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Save the results
|
||||||
|
timer.stop();
|
||||||
|
result = "The number of safe reports is " + safeReports + ".\nIt took " + timer.toString() + " to run the algorithm.";
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isSafeReport(int[] elements, boolean recursive){
|
||||||
|
boolean isSafe = false;
|
||||||
|
//Check if the array is long enough
|
||||||
|
if(elements.length <= 1){
|
||||||
|
isSafe = true;
|
||||||
|
}
|
||||||
|
//Check if the first tow elements are ascending or descending
|
||||||
|
else if(elements[0] > elements[1]){
|
||||||
|
isSafe = isSafeDescending(elements);
|
||||||
|
}
|
||||||
|
else if(elements[0] < elements[1]){
|
||||||
|
isSafe = isSafeAscending(elements);
|
||||||
|
}
|
||||||
|
//If the elements are the same that violates a rule, so it is unsafe
|
||||||
|
else{
|
||||||
|
isSafe = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return isSafe;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isSafeAscending(int[] elements){
|
||||||
|
if(elements.length <= 1){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int lastElement = elements[0];
|
||||||
|
for(int cnt = 1;cnt < elements.length;++cnt){
|
||||||
|
int currentElement = elements[cnt];
|
||||||
|
//If one of the elements is less than or equal to the last, then it is not ascending
|
||||||
|
if(currentElement <= lastElement){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//If one of the elements is more than 3 larger than the last, then it is unsafe
|
||||||
|
else if((currentElement - 3) > lastElement){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//If neither of the above triggers is hit then these two elements are safe, proceed to the next
|
||||||
|
else{
|
||||||
|
lastElement = currentElement;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//If the function was never triggered to return false, then return true
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isSafeDescending(int[] elements){
|
||||||
|
if(elements.length <= 1){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int lastElement = elements[0];
|
||||||
|
for(int cnt = 1;cnt < elements.length;++cnt){
|
||||||
|
int currentElement = elements[cnt];
|
||||||
|
//If one of the elements is greater than or equal to the last, then it is not descending
|
||||||
|
if(currentElement >= lastElement){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//If one of the elements is more than 3 smaller than the last, then it is unsafe
|
||||||
|
else if((currentElement + 3) < lastElement){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//If neither of the above trigger is hit then these two elements are safe, proceed to the next
|
||||||
|
else{
|
||||||
|
lastElement = currentElement;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//If the function was never triggered to return false, then return true
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int[] convertReportToArray(String report){
|
||||||
|
//Split the report on space to get the individual elements
|
||||||
|
String[] elementsString = report.split(" ");
|
||||||
|
//Convert the elements to integers
|
||||||
|
int[] elements = new int[elementsString.length];
|
||||||
|
for(int cnt = 0;cnt < elementsString.length;++cnt){
|
||||||
|
elements[cnt] = Integer.parseInt(elementsString[cnt]);
|
||||||
|
}
|
||||||
|
return elements;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Find the "safe" reports from the given list.
|
||||||
|
The number of safe reports is 502.
|
||||||
|
It took 15.010 milliseconds to run the algorithm.
|
||||||
|
*/
|
||||||
161
src/main/java/com/mattrixwv/adventOfCode24/days/Problem4.java
Normal file
161
src/main/java/com/mattrixwv/adventOfCode24/days/Problem4.java
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
package com.mattrixwv.adventOfCode24.days;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import com.mattrixwv.Stopwatch;
|
||||||
|
|
||||||
|
|
||||||
|
public class Problem4 extends Problem{
|
||||||
|
private static final String inputFileName = "days/Problem3.txt";
|
||||||
|
|
||||||
|
|
||||||
|
public Problem4(){
|
||||||
|
super();
|
||||||
|
description = "Find the \"safe\" reports from the given list.";
|
||||||
|
result = "Unresolved";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String runSolution(){
|
||||||
|
Stopwatch timer = new Stopwatch();
|
||||||
|
timer.start();
|
||||||
|
|
||||||
|
List<String> reports = new ArrayList<>();
|
||||||
|
int safeReports = 0;
|
||||||
|
|
||||||
|
|
||||||
|
//Read elements from the file
|
||||||
|
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||||
|
scanner.useDelimiter("\n");
|
||||||
|
while(scanner.hasNext()){
|
||||||
|
String report = scanner.next();
|
||||||
|
if(!report.isBlank()){
|
||||||
|
reports.add(report);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Count how many reports are safe
|
||||||
|
for(String report : reports){
|
||||||
|
if(isSafeReport(convertReportToArray(report), false)){
|
||||||
|
++safeReports;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Save the results
|
||||||
|
timer.stop();
|
||||||
|
result = "The number of safe reports is " + safeReports + ".\nIt took " + timer.toString() + " to run the algorithm.";
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isSafeReport(int[] elements, boolean recursive){
|
||||||
|
boolean isSafe = false;
|
||||||
|
//Check if the array is long enough
|
||||||
|
if(elements.length <= 1){
|
||||||
|
isSafe = true;
|
||||||
|
}
|
||||||
|
//Check if the first tow elements are ascending or descending
|
||||||
|
else if(elements[0] > elements[1]){
|
||||||
|
isSafe = isSafeDescending(elements);
|
||||||
|
for(int cnt = 0;(!recursive) && (!isSafe) && (cnt < elements.length);++cnt){
|
||||||
|
isSafe = isSafeReport(removeElement(elements, cnt), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(elements[0] < elements[1]){
|
||||||
|
isSafe = isSafeAscending(elements);
|
||||||
|
for(int cnt = 0;(!recursive) && (!isSafe) && (cnt < elements.length);++cnt){
|
||||||
|
isSafe = isSafeReport(removeElement(elements, cnt), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//If the elements are the same that violates a rule, so it is unsafe
|
||||||
|
else{
|
||||||
|
for(int cnt = 0;(!recursive) && (!isSafe) && (cnt < elements.length);++cnt){
|
||||||
|
isSafe = isSafeReport(removeElement(elements, cnt), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return isSafe;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isSafeAscending(int[] elements){
|
||||||
|
if(elements.length <= 1){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int lastElement = elements[0];
|
||||||
|
for(int cnt = 1;cnt < elements.length;++cnt){
|
||||||
|
int currentElement = elements[cnt];
|
||||||
|
//If one of the elements is less than or equal to the last, then it is not ascending
|
||||||
|
if(currentElement <= lastElement){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//If one of the elements is more than 3 larger than the last, then it is unsafe
|
||||||
|
else if((currentElement - 3) > lastElement){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//If neither of the above triggers is hit then these two elements are safe, proceed to the next
|
||||||
|
else{
|
||||||
|
lastElement = currentElement;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//If the function was never triggered to return false, then return true
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isSafeDescending(int[] elements){
|
||||||
|
if(elements.length <= 1){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int lastElement = elements[0];
|
||||||
|
for(int cnt = 1;cnt < elements.length;++cnt){
|
||||||
|
int currentElement = elements[cnt];
|
||||||
|
//If one of the elements is greater than or equal to the last, then it is not descending
|
||||||
|
if(currentElement >= lastElement){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//If one of the elements is more than 3 smaller than the last, then it is unsafe
|
||||||
|
else if((currentElement + 3) < lastElement){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//If neither of the above trigger is hit then these two elements are safe, proceed to the next
|
||||||
|
else{
|
||||||
|
lastElement = currentElement;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//If the function was never triggered to return false, then return true
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int[] removeElement(int[] elements, int location){
|
||||||
|
int[] newElements = new int[elements.length - 1];
|
||||||
|
int newCnt = 0;
|
||||||
|
for(int cnt = 0;cnt < elements.length;++cnt){
|
||||||
|
if(cnt != location){
|
||||||
|
newElements[newCnt++] = elements[cnt];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newElements;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int[] convertReportToArray(String report){
|
||||||
|
//Split the report on space to get the individual elements
|
||||||
|
String[] elementsString = report.split(" ");
|
||||||
|
//Convert the elements to integers
|
||||||
|
int[] elements = new int[elementsString.length];
|
||||||
|
for(int cnt = 0;cnt < elementsString.length;++cnt){
|
||||||
|
elements[cnt] = Integer.parseInt(elementsString[cnt]);
|
||||||
|
}
|
||||||
|
return elements;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Find the "safe" reports from the given list.
|
||||||
|
The number of safe reports is 544.
|
||||||
|
It took 5.181 milliseconds to run the algorithm.
|
||||||
|
*/
|
||||||
1000
src/main/resources/days/Problem3.txt
Normal file
1000
src/main/resources/days/Problem3.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user