Complete day 2

This commit is contained in:
2024-12-09 20:44:54 -05:00
parent 664a02115d
commit ba221663cb
5 changed files with 1310 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
package com.mattrixwv.adventOfCode24.days;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -7,6 +8,7 @@ import java.util.Scanner;
import com.mattrixwv.Stopwatch;
public class Problem2 extends Problem{
private static final String inputFileName = "days/Problem1.txt";

View 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.
*/

View 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.
*/