Complete day 5
This commit is contained in:
@@ -8,6 +8,7 @@ import java.util.StringJoiner;
|
||||
|
||||
import com.mattrixwv.adventOfCode24.days.Problem;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem1;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem10;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem2;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem3;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem4;
|
||||
@@ -15,12 +16,13 @@ import com.mattrixwv.adventOfCode24.days.Problem5;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem6;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem7;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem8;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem9;
|
||||
|
||||
|
||||
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, 7, 8);
|
||||
protected static final List<Integer> PROBLEM_NUMBERS = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
|
||||
|
||||
private ProblemSelector(){
|
||||
@@ -40,6 +42,8 @@ public class ProblemSelector{
|
||||
case 6 : day = new Problem6(); break;
|
||||
case 7 : day = new Problem7(); break;
|
||||
case 8 : day = new Problem8(); break;
|
||||
case 9 : day = new Problem9(); break;
|
||||
case 10 : day = new Problem10(); break;
|
||||
default: throw new InvalidParameterException();
|
||||
}
|
||||
return day;
|
||||
|
||||
116
src/main/java/com/mattrixwv/adventOfCode24/days/Problem10.java
Normal file
116
src/main/java/com/mattrixwv/adventOfCode24/days/Problem10.java
Normal file
@@ -0,0 +1,116 @@
|
||||
package com.mattrixwv.adventOfCode24.days;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import com.mattrixwv.Stopwatch;
|
||||
import com.mattrixwv.Triple;
|
||||
|
||||
|
||||
public class Problem10 extends Problem{
|
||||
private static final String inputFileName = "days/Problem9.txt";
|
||||
private List<Triple<Integer, Integer, Integer>> orderList;
|
||||
private List<List<Integer>> printingList;
|
||||
|
||||
|
||||
public Problem10(){
|
||||
super();
|
||||
description = "Find the sum of the middle pages of all of the corrected prints.";
|
||||
result = "Unresolved";
|
||||
}
|
||||
|
||||
public String runSolution(){
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.start();
|
||||
|
||||
String orderInfo = "";
|
||||
String printingInfo = "";
|
||||
int sum = 0;
|
||||
|
||||
|
||||
//Read the file
|
||||
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||
scanner.useDelimiter("\n\n");
|
||||
//Get the page ordering info
|
||||
orderInfo = scanner.next();
|
||||
//Get the page printing info
|
||||
printingInfo = scanner.next();
|
||||
}
|
||||
//Read the page ordering info into a list of pairs
|
||||
orderList = new ArrayList<>();
|
||||
for(String order : orderInfo.split("\n")){
|
||||
String[] elements = order.split("\\|");
|
||||
orderList.add(new Triple<Integer, Integer, Integer>(Integer.parseInt(elements[0]), Integer.parseInt(elements[1]), 0));
|
||||
}
|
||||
//Read the printing info into a list of lists
|
||||
printingList = new ArrayList<>();
|
||||
for(String print : printingInfo.split("\n")){
|
||||
String[] elements = print.split(",");
|
||||
ArrayList<Integer> elementList = new ArrayList<>();
|
||||
for(String element : elements){
|
||||
elementList.add(Integer.parseInt(element));
|
||||
}
|
||||
printingList.add(elementList);
|
||||
}
|
||||
|
||||
//Iterate through each printing list, testing them
|
||||
List<List<Integer>> failedPrints = new ArrayList<>();
|
||||
for(List<Integer> print : printingList){
|
||||
//If it doesn't pass the rules tests then add it to the new list
|
||||
if(!passesRules(print)){
|
||||
failedPrints.add(print);
|
||||
}
|
||||
}
|
||||
|
||||
//Take the failed prints and put them in the correct order
|
||||
for(List<Integer> print : failedPrints){
|
||||
//Once a print is in the correct order, sum the middle numbers
|
||||
List<Integer> correctedPrint = correctPrintOrder(print);
|
||||
sum += correctedPrint.get(correctedPrint.size() / 2);
|
||||
}
|
||||
|
||||
|
||||
//Save the results
|
||||
timer.stop();
|
||||
result = "The sum of the middle pages of all of the corrected prints is " + sum + ".\nIt took " + timer.toString() + " to run the algorithm.";
|
||||
return result;
|
||||
}
|
||||
|
||||
private boolean passesRules(List<Integer> print){
|
||||
//Iterate through each rule and test it against the current print list
|
||||
for(Triple<Integer, Integer, Integer> order : orderList){
|
||||
int loc1 = print.indexOf(order.getA());
|
||||
int loc2 = print.indexOf(order.getB());
|
||||
//If both numbers exist in the print list and 1 comes after 2, the rule failed
|
||||
if((loc1 >= 0) && (loc2 >= 0) && (loc1 > loc2)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//If it didn't fail any rules then it passes
|
||||
return true;
|
||||
}
|
||||
|
||||
private List<Integer> correctPrintOrder(List<Integer> print){
|
||||
//Iterate through each rule and test it against the current print list
|
||||
for(Triple<Integer, Integer, Integer> order : orderList){
|
||||
int loc1 = print.indexOf(order.getA());
|
||||
int loc2 = print.indexOf(order.getB());
|
||||
//If both numbers exist in the print list and 1 comes after 2, the rule failed
|
||||
if((loc1 >= 0) && (loc2 >= 0) && (loc1 > loc2)){
|
||||
print.set(loc1, order.getB());
|
||||
print.set(loc2, order.getA());
|
||||
return correctPrintOrder(print);
|
||||
}
|
||||
}
|
||||
return print;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Find the sum of the middle pages of all of the corrected prints.
|
||||
The sum of the middle pages of all of the corrected prints is 4480.
|
||||
It took 39.695 milliseconds to run the algorithm.
|
||||
*/
|
||||
@@ -0,0 +1,93 @@
|
||||
package com.mattrixwv.adventOfCode24.days;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import com.mattrixwv.Stopwatch;
|
||||
import com.mattrixwv.Triple;
|
||||
|
||||
|
||||
public class Problem9 extends Problem{
|
||||
private static final String inputFileName = "days/Problem9.txt";
|
||||
private List<Triple<Integer, Integer, Integer>> orderList;
|
||||
private List<List<Integer>> printingList;
|
||||
|
||||
|
||||
public Problem9(){
|
||||
super();
|
||||
description = "Find the sum of the middle pages of all of the correctly ordered prints.";
|
||||
result = "Unresolved";
|
||||
}
|
||||
|
||||
public String runSolution(){
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.start();
|
||||
|
||||
String orderInfo = "";
|
||||
String printingInfo = "";
|
||||
int sum = 0;
|
||||
|
||||
|
||||
//Read the file
|
||||
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||
scanner.useDelimiter("\n\n");
|
||||
//Get the page ordering info
|
||||
orderInfo = scanner.next();
|
||||
//Get the page printing info
|
||||
printingInfo = scanner.next();
|
||||
}
|
||||
//Read the page ordering info into a list of pairs
|
||||
orderList = new ArrayList<>();
|
||||
for(String order : orderInfo.split("\n")){
|
||||
String[] elements = order.split("\\|");
|
||||
orderList.add(new Triple<Integer, Integer, Integer>(Integer.parseInt(elements[0]), Integer.parseInt(elements[1]), 0));
|
||||
}
|
||||
//Read the printing info into a list of lists
|
||||
printingList = new ArrayList<>();
|
||||
for(String print : printingInfo.split("\n")){
|
||||
String[] elements = print.split(",");
|
||||
ArrayList<Integer> elementList = new ArrayList<>();
|
||||
for(String element : elements){
|
||||
elementList.add(Integer.parseInt(element));
|
||||
}
|
||||
printingList.add(elementList);
|
||||
}
|
||||
|
||||
//Iterate through each printing list, testing them
|
||||
for(List<Integer> print : printingList){
|
||||
//If it passes all rule tests then add the middle page to the sum
|
||||
if(passesRules(print)){
|
||||
sum += print.get(print.size() / 2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Save the results
|
||||
timer.stop();
|
||||
result = "The sum of the middle pages of all of the correctly ordered prints is " + sum + ".\nIt took " + timer.toString() + " to run the algorithm.";
|
||||
return result;
|
||||
}
|
||||
|
||||
private boolean passesRules(List<Integer> print){
|
||||
//Iterate through each rule and test it against the current print list
|
||||
for(Triple<Integer, Integer, Integer> order : orderList){
|
||||
int loc1 = print.indexOf(order.getA());
|
||||
int loc2 = print.indexOf(order.getB());
|
||||
//If both numbers exist in the print list and 1 comes after 2, the rule failed
|
||||
if((loc1 >= 0) && (loc2 >= 0) && (loc1 > loc2)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//If it didn't fail any rules then it passes
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Find the sum of the middle pages of all of the correctly ordered prints.
|
||||
The sum of the middle pages of all of the correctly ordered prints is 4185.
|
||||
It took 16.125 milliseconds to run the algorithm.
|
||||
*/
|
||||
1352
src/main/resources/days/Problem9.txt
Normal file
1352
src/main/resources/days/Problem9.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user