Complete day 7
This commit is contained in:
118
src/main/java/com/mattrixwv/adventOfCode24/days/Problem13.java
Normal file
118
src/main/java/com/mattrixwv/adventOfCode24/days/Problem13.java
Normal file
@@ -0,0 +1,118 @@
|
||||
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 Problem13 extends Problem{
|
||||
private static final String inputFileName = "days/Problem13.txt";
|
||||
private List<Triple<Long, List<Long>, List<Character>>> values;
|
||||
|
||||
|
||||
public Problem13(){
|
||||
super();
|
||||
description = "Find the sum of the answers of the equations that have solutions.";
|
||||
result = "Unresolved";
|
||||
}
|
||||
|
||||
public String runSolution(){
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.start();
|
||||
|
||||
|
||||
//Read the file
|
||||
values = new ArrayList<>();
|
||||
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||
scanner.useDelimiter("\n");
|
||||
while(scanner.hasNext()){
|
||||
String inputLine = scanner.next();
|
||||
if(inputLine.isBlank()){
|
||||
continue;
|
||||
}
|
||||
long answer = Long.parseLong(inputLine.split(":")[0]);
|
||||
List<Long> elements = new ArrayList<>();
|
||||
String elementsString = inputLine.split(":")[1];
|
||||
for(String elementString : elementsString.split(" ")){
|
||||
if(elementString.isBlank()){
|
||||
continue;
|
||||
}
|
||||
long element = Long.parseLong(elementString);
|
||||
elements.add(element);
|
||||
}
|
||||
List<Character> equations = new ArrayList<>();
|
||||
for(int cnt = 0;cnt < elements.size() - 1;++cnt){
|
||||
equations.add('+');
|
||||
}
|
||||
values.add(new Triple<Long, List<Long>, List<Character>>(answer, elements, equations));
|
||||
}
|
||||
}
|
||||
|
||||
long sum = 0;
|
||||
for(Triple<Long, List<Long>, List<Character>> value : values){
|
||||
if(hasSolution(value)){
|
||||
sum += value.getA();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Save the result
|
||||
timer.stop();
|
||||
result = "The sum of the answers of the equations that have solutions is " + sum + ".\nIt took " + timer.toString() + " to run the algorithm.";
|
||||
return result;
|
||||
}
|
||||
|
||||
private boolean hasSolution(Triple<Long, List<Long>, List<Character>> value){
|
||||
//Loop through all possibilities for the equations and return true if one matches
|
||||
for(int cnt = 0;cnt < (Math.pow(2, value.getC().size()));++cnt){
|
||||
long equationValue = value.getB().get(0);
|
||||
for(int eqnCnt = 0;eqnCnt < value.getC().size();++eqnCnt){
|
||||
Character eqnIndicator = value.getC().get(eqnCnt);
|
||||
if(eqnIndicator == '+'){
|
||||
equationValue += value.getB().get(eqnCnt + 1);
|
||||
}
|
||||
else if(eqnIndicator == '*'){
|
||||
equationValue *= value.getB().get(eqnCnt + 1);
|
||||
}
|
||||
}
|
||||
//If the values match then return true
|
||||
if(equationValue == value.getA()){
|
||||
return true;
|
||||
}
|
||||
//If they don't match, advance to the next equation iteration
|
||||
else{
|
||||
advanceEquations(value.getC());
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void advanceEquations(List<Character> eqn){
|
||||
advanceEquations(eqn, 0);
|
||||
}
|
||||
|
||||
private void advanceEquations(List<Character> eqn, int index){
|
||||
if(index >= eqn.size()){
|
||||
return;
|
||||
}
|
||||
|
||||
if(eqn.get(index) == '+'){
|
||||
eqn.set(index, '*');
|
||||
}
|
||||
else if(eqn.get(index) == '*'){
|
||||
eqn.set(index, '+');
|
||||
advanceEquations(eqn, index + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Find the sum of the answers of the equations that have solutions.
|
||||
The sum of the answers of the equations that have solutions is 42283209483350.
|
||||
It took 56.421 milliseconds to run the algorithm.
|
||||
*/
|
||||
124
src/main/java/com/mattrixwv/adventOfCode24/days/Problem14.java
Normal file
124
src/main/java/com/mattrixwv/adventOfCode24/days/Problem14.java
Normal file
@@ -0,0 +1,124 @@
|
||||
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 Problem14 extends Problem{
|
||||
private static final String inputFileName = "days/Problem13.txt";
|
||||
private List<Triple<Long, List<Long>, List<Character>>> values;
|
||||
|
||||
|
||||
public Problem14(){
|
||||
super();
|
||||
description = "Find the sum of the answers of the equations that have solutions.";
|
||||
result = "Unresolved";
|
||||
}
|
||||
|
||||
public String runSolution(){
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.start();
|
||||
|
||||
|
||||
//Read the file
|
||||
values = new ArrayList<>();
|
||||
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||
scanner.useDelimiter("\n");
|
||||
while(scanner.hasNext()){
|
||||
String inputLine = scanner.next();
|
||||
if(inputLine.isBlank()){
|
||||
continue;
|
||||
}
|
||||
long answer = Long.parseLong(inputLine.split(":")[0]);
|
||||
List<Long> elements = new ArrayList<>();
|
||||
String elementsString = inputLine.split(":")[1];
|
||||
for(String elementString : elementsString.split(" ")){
|
||||
if(elementString.isBlank()){
|
||||
continue;
|
||||
}
|
||||
long element = Long.parseLong(elementString);
|
||||
elements.add(element);
|
||||
}
|
||||
List<Character> equations = new ArrayList<>();
|
||||
for(int cnt = 0;cnt < elements.size() - 1;++cnt){
|
||||
equations.add('+');
|
||||
}
|
||||
values.add(new Triple<Long, List<Long>, List<Character>>(answer, elements, equations));
|
||||
}
|
||||
}
|
||||
|
||||
long sum = 0;
|
||||
for(Triple<Long, List<Long>, List<Character>> value : values){
|
||||
if(hasSolution(value)){
|
||||
sum += value.getA();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Save the result
|
||||
timer.stop();
|
||||
result = "The sum of the answers of the equations that have solutions is " + sum + ".\nIt took " + timer.toString() + " to run the algorithm.";
|
||||
return result;
|
||||
}
|
||||
|
||||
private boolean hasSolution(Triple<Long, List<Long>, List<Character>> value){
|
||||
//Loop through all possibilities for the equations and return true if one matches
|
||||
for(int cnt = 0;cnt < (Math.pow(3, value.getC().size()));++cnt){
|
||||
long equationValue = value.getB().get(0);
|
||||
for(int eqnCnt = 0;eqnCnt < value.getC().size();++eqnCnt){
|
||||
Character eqnIndicator = value.getC().get(eqnCnt);
|
||||
if(eqnIndicator == '+'){
|
||||
equationValue += value.getB().get(eqnCnt + 1);
|
||||
}
|
||||
else if(eqnIndicator == '*'){
|
||||
equationValue *= value.getB().get(eqnCnt + 1);
|
||||
}
|
||||
else if(eqnIndicator == '|'){
|
||||
equationValue = Long.parseLong(Long.toString(equationValue) + Long.toString(value.getB().get(eqnCnt + 1)));
|
||||
}
|
||||
}
|
||||
//If the values match then return true
|
||||
if(equationValue == value.getA()){
|
||||
return true;
|
||||
}
|
||||
//If they don't match, advance to the next equation
|
||||
else{
|
||||
advanceEquations(value.getC());
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void advanceEquations(List<Character> eqn){
|
||||
advanceEquations(eqn, 0);
|
||||
}
|
||||
|
||||
private void advanceEquations(List<Character> eqn, int index){
|
||||
if(index >= eqn.size()){
|
||||
return;
|
||||
}
|
||||
|
||||
if(eqn.get(index) == '+'){
|
||||
eqn.set(index, '*');
|
||||
}
|
||||
else if(eqn.get(index) == '*'){
|
||||
eqn.set(index, '|');
|
||||
}
|
||||
else if(eqn.get(index) == '|'){
|
||||
eqn.set(index, '+');
|
||||
advanceEquations(eqn, index + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Find the sum of the answers of the equations that have solutions.
|
||||
The sum of the answers of the equations that have solutions is 1026766857276279.
|
||||
It took 1.238 seconds to run the algorithm.
|
||||
*/
|
||||
Reference in New Issue
Block a user