Complete day 3
This commit is contained in:
@@ -11,12 +11,14 @@ import com.mattrixwv.adventOfCode24.days.Problem1;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem2;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem3;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem4;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem5;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem6;
|
||||
|
||||
|
||||
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);
|
||||
protected static final List<Integer> PROBLEM_NUMBERS = List.of(0, 1, 2, 3, 4, 5, 6);
|
||||
|
||||
|
||||
private ProblemSelector(){
|
||||
@@ -32,6 +34,8 @@ public class ProblemSelector{
|
||||
case 2 : day = new Problem2(); break;
|
||||
case 3 : day = new Problem3(); break;
|
||||
case 4 : day = new Problem4(); break;
|
||||
case 5 : day = new Problem5(); break;
|
||||
case 6 : day = new Problem6(); break;
|
||||
default: throw new InvalidParameterException();
|
||||
}
|
||||
return day;
|
||||
|
||||
@@ -15,7 +15,7 @@ public class Problem2 extends Problem{
|
||||
|
||||
public Problem2(){
|
||||
super();
|
||||
description = "Find the similarity score between the two columsn of numbers";
|
||||
description = "Find the similarity score between the two columns of numbers.";
|
||||
result = "Unresolved";
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.mattrixwv.adventOfCode24.days;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.mattrixwv.Stopwatch;
|
||||
|
||||
|
||||
public class Problem5 extends Problem{
|
||||
private static final String inputFileName = "days/Problem5.txt";
|
||||
|
||||
|
||||
public Problem5(){
|
||||
super();
|
||||
description = "Find the non-corrupted multiplication instructions that are allowed to run.";
|
||||
result = "Unresolved";
|
||||
}
|
||||
|
||||
public String runSolution(){
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.start();
|
||||
|
||||
int sum = 0;
|
||||
StringBuilder fileInput = new StringBuilder();
|
||||
|
||||
|
||||
//Read the file
|
||||
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||
while(scanner.hasNext()){
|
||||
fileInput.append(scanner.next());
|
||||
}
|
||||
}
|
||||
|
||||
//Pull out the valid references from the file
|
||||
Pattern functionPattern = Pattern.compile("mul\\(\\d{1,3},\\d{1,3}\\)");
|
||||
Matcher functionMatcher = functionPattern.matcher(fileInput.toString());
|
||||
List<String> functions = new ArrayList<>();
|
||||
while(functionMatcher.find()){
|
||||
functions.add(functionMatcher.group());
|
||||
}
|
||||
StringBuilder functionString = new StringBuilder();
|
||||
functions.forEach(func -> functionString.append(func));
|
||||
|
||||
//Perform the required multiplication
|
||||
Pattern digitPattern = Pattern.compile("\\d{1,3}");
|
||||
Matcher digitMatcher = digitPattern.matcher(functionString.toString());
|
||||
while(digitMatcher.find()){
|
||||
int num1 = Integer.parseInt(digitMatcher.group());
|
||||
digitMatcher.find();
|
||||
int num2 = Integer.parseInt(digitMatcher.group());
|
||||
sum += (num1 * num2);
|
||||
}
|
||||
|
||||
|
||||
//Save the results
|
||||
timer.stop();
|
||||
result = "The sum of the non-corrupted multiplication instructions is " + sum + ".\nIt took " + timer.toString() + " to run the algorithm.";
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Find the non-corrupted multiplication instructions that are allowed to run
|
||||
The sum of the non-corrupted multiplication instructions is 168539636.
|
||||
It took 13.816 milliseconds to run the algorithm.
|
||||
*/
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.mattrixwv.adventOfCode24.days;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.mattrixwv.Stopwatch;
|
||||
|
||||
|
||||
public class Problem6 extends Problem{
|
||||
private static final String inputFileName = "days/Problem5.txt";
|
||||
|
||||
|
||||
public Problem6(){
|
||||
super();
|
||||
description = "Find the non-corrupted multiplication instructions that are allowed to run.";
|
||||
result = "Unresolved";
|
||||
}
|
||||
|
||||
public String runSolution(){
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.start();
|
||||
|
||||
int sum = 0;
|
||||
StringBuilder fileInput = new StringBuilder();
|
||||
|
||||
|
||||
//Read the file
|
||||
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||
while(scanner.hasNext()){
|
||||
fileInput.append(scanner.next());
|
||||
}
|
||||
}
|
||||
|
||||
//Pull out the valid references from the file
|
||||
Pattern functionPattern = Pattern.compile("mul\\(\\d{1,3},\\d{1,3}\\)|do\\(\\)|don't\\(\\)");
|
||||
Matcher functionMatcher = functionPattern.matcher(fileInput.toString());
|
||||
List<String> functions = new ArrayList<>();
|
||||
while(functionMatcher.find()){
|
||||
functions.add(functionMatcher.group());
|
||||
}
|
||||
|
||||
//Perform the required multiplication
|
||||
boolean enabled = true;
|
||||
Pattern digitPattern = Pattern.compile("\\d{1,3}");
|
||||
for(String function : functions){
|
||||
if(!enabled){
|
||||
enabled = function.equals("do()");
|
||||
}
|
||||
else{
|
||||
if(function.equals("do()")){
|
||||
continue;
|
||||
}
|
||||
else if(function.equals("don't()")){
|
||||
enabled = false;
|
||||
}
|
||||
else{
|
||||
Matcher digitMatcher = digitPattern.matcher(function);
|
||||
digitMatcher.find();
|
||||
int num1 = Integer.parseInt(digitMatcher.group());
|
||||
digitMatcher.find();
|
||||
int num2 = Integer.parseInt(digitMatcher.group());
|
||||
sum += (num1 * num2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Save the results
|
||||
timer.stop();
|
||||
result = "The sum of the non-corrupted multiplication instructions is " + sum + ".\nIt took " + timer.toString() + " to run the algorithm.";
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Find the non-corrupted multiplication instructions that are allowed to run
|
||||
The sum of the non-corrupted multiplication instructions is 97529391.
|
||||
It took 4.187 milliseconds to run the algorithm.
|
||||
*/
|
||||
Reference in New Issue
Block a user