Complete day 11
This commit is contained in:
@@ -20,6 +20,8 @@ import com.mattrixwv.adventOfCode24.days.Problem18;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem19;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem2;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem20;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem21;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem22;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem3;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem4;
|
||||
import com.mattrixwv.adventOfCode24.days.Problem5;
|
||||
@@ -35,7 +37,7 @@ public class ProblemSelector{
|
||||
protected static final List<Integer> PROBLEM_NUMBERS = List.of(
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
|
||||
20
|
||||
20, 21, 22
|
||||
);
|
||||
|
||||
|
||||
@@ -68,6 +70,8 @@ public class ProblemSelector{
|
||||
case 18 : day = new Problem18(); break;
|
||||
case 19 : day = new Problem19(); break;
|
||||
case 20 : day = new Problem20(); break;
|
||||
case 21 : day = new Problem21(); break;
|
||||
case 22 : day = new Problem22(); break;
|
||||
default: throw new InvalidParameterException();
|
||||
}
|
||||
return day;
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.mattrixwv.adventOfCode24.days;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import com.mattrixwv.Stopwatch;
|
||||
|
||||
|
||||
public class Problem21 extends Problem{
|
||||
private static final String inputFileName = "days/Problem21.txt";
|
||||
private static final int numIterations = 25;
|
||||
private List<Long> stoneLine;
|
||||
|
||||
|
||||
public Problem21(){
|
||||
super();
|
||||
description = "Find the number of stones after a given number of iterations.";
|
||||
result = "Unresolved";
|
||||
}
|
||||
|
||||
|
||||
public String runSolution(){
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.start();
|
||||
|
||||
|
||||
//Read the file
|
||||
stoneLine = new ArrayList<>();
|
||||
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||
scanner.useDelimiter(" ");
|
||||
while(scanner.hasNext()){
|
||||
String nextNum = scanner.next().strip();
|
||||
if(!nextNum.isBlank()){
|
||||
stoneLine.add(Long.parseLong(nextNum));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for(int cnt = 0;cnt < numIterations;++cnt){
|
||||
stoneLine = performIteration(stoneLine);
|
||||
}
|
||||
|
||||
|
||||
//Save the result
|
||||
timer.stop();
|
||||
result = "There are " + stoneLine.size() + " stones after " + numIterations + " iterations.\nIt took " + timer.toString() + " to run this algorithm.";
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
private List<Long> performIteration(List<Long> stoneLine){
|
||||
List<Long> newStoneLine = new ArrayList<>();
|
||||
for(Long num : stoneLine){
|
||||
if(num == 0){
|
||||
newStoneLine.add(1L);
|
||||
}
|
||||
else if((num.toString().length() % 2) == 0){
|
||||
String numString = num.toString();
|
||||
String num1String = numString.substring(0, numString.length() / 2);
|
||||
String num2String = numString.substring(numString.length() / 2);
|
||||
newStoneLine.add(Long.parseLong(num1String));
|
||||
newStoneLine.add(Long.parseLong(num2String));
|
||||
}
|
||||
else{
|
||||
newStoneLine.add(num * 2024);
|
||||
}
|
||||
}
|
||||
return newStoneLine;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Find the number of stones after a given number of iterations.
|
||||
There are 193269 stones after 25 iterations.
|
||||
It took 47.751 milliseconds to run this algorithm.
|
||||
*/
|
||||
121
src/main/java/com/mattrixwv/adventOfCode24/days/Problem22.java
Normal file
121
src/main/java/com/mattrixwv/adventOfCode24/days/Problem22.java
Normal file
@@ -0,0 +1,121 @@
|
||||
package com.mattrixwv.adventOfCode24.days;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Scanner;
|
||||
|
||||
import com.mattrixwv.Stopwatch;
|
||||
|
||||
|
||||
public class Problem22 extends Problem{
|
||||
private static final String inputFileName = "days/Problem21.txt";
|
||||
private static final int numIterations = 75;
|
||||
private Map<Long, Long> stoneLine;
|
||||
|
||||
|
||||
public Problem22(){
|
||||
super();
|
||||
description = "Find the number of stones after a given number of iterations.";
|
||||
result = "Unresolved";
|
||||
}
|
||||
|
||||
|
||||
public String runSolution(){
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.start();
|
||||
|
||||
|
||||
//Read the file
|
||||
/* */
|
||||
stoneLine = new HashMap<>();
|
||||
try(Scanner scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream(inputFileName))){
|
||||
scanner.useDelimiter(" ");
|
||||
while(scanner.hasNext()){
|
||||
String nextNum = scanner.next().strip();
|
||||
if(!nextNum.isBlank()){
|
||||
Long num = Long.parseLong(nextNum);
|
||||
if(stoneLine.containsKey(num)){
|
||||
stoneLine.put(num, stoneLine.get(num) + 1L);
|
||||
}
|
||||
else{
|
||||
stoneLine.put(num, 1L);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/* */
|
||||
/*
|
||||
stoneLine = new HashMap<>();
|
||||
stoneLine.put(125L, 1L);
|
||||
stoneLine.put(17L, 1L);
|
||||
/* */
|
||||
|
||||
|
||||
for(int cnt = 0;cnt < numIterations;++cnt){
|
||||
stoneLine = performIteration(stoneLine);
|
||||
//System.out.println("New stone line = " + stoneLine);
|
||||
//System.out.println("Iteration " + (cnt + 1));
|
||||
}
|
||||
Long sum = 0L;
|
||||
for(Entry<Long, Long> entry : stoneLine.entrySet()){
|
||||
sum += entry.getValue();
|
||||
}
|
||||
|
||||
|
||||
//Save the result
|
||||
timer.stop();
|
||||
result = "There are " + sum + " stones after " + numIterations + " iterations.\nIt took " + timer.toString() + " to run this algorithm.";
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
private Map<Long, Long> performIteration(Map<Long, Long> stoneLine){
|
||||
Map<Long, Long> newStoneLine = new HashMap<>();
|
||||
for(Entry<Long, Long> entry : stoneLine.entrySet()){
|
||||
Long num = entry.getKey();
|
||||
Long value = entry.getValue();
|
||||
|
||||
if(num == 0){
|
||||
newStoneLine.put(1L, value);
|
||||
}
|
||||
else if((num.toString().length() % 2) == 0){
|
||||
String numString = num.toString();
|
||||
String num1String = numString.substring(0, numString.length() / 2);
|
||||
String num2String = numString.substring(numString.length() / 2);
|
||||
Long num1 = Long.parseLong(num1String);
|
||||
Long num2 = Long.parseLong(num2String);
|
||||
if(newStoneLine.containsKey(num1)){
|
||||
newStoneLine.put(num1, newStoneLine.get(num1) + value);
|
||||
}
|
||||
else{
|
||||
newStoneLine.put(num1, value);
|
||||
}
|
||||
if(newStoneLine.containsKey(num2)){
|
||||
newStoneLine.put(num2, newStoneLine.get(num2) + value);
|
||||
}
|
||||
else{
|
||||
newStoneLine.put(num2, value);
|
||||
}
|
||||
}
|
||||
else{
|
||||
Long newNum = num * 2024;
|
||||
if(newStoneLine.containsKey(newNum)){
|
||||
newStoneLine.put(newNum, newStoneLine.get(newNum) + value);
|
||||
}
|
||||
else{
|
||||
newStoneLine.put(newNum, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return newStoneLine;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Find the number of stones after a given number of iterations.
|
||||
There are 228449040027793 stones after 75 iterations.
|
||||
It took 69.644 milliseconds to run this algorithm.
|
||||
*/
|
||||
1
src/main/resources/days/Problem21.txt
Normal file
1
src/main/resources/days/Problem21.txt
Normal file
@@ -0,0 +1 @@
|
||||
475449 2599064 213 0 2 65 5755 51149
|
||||
Reference in New Issue
Block a user