From a4dfc8f5977edc1c3a56c5fd42f05c678f9ada0a Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Thu, 19 Dec 2024 17:36:41 -0500 Subject: [PATCH] Complete day 11 --- .../adventOfCode24/ProblemSelector.java | 6 +- .../adventOfCode24/days/Problem21.java | 80 ++++++++++++ .../adventOfCode24/days/Problem22.java | 121 ++++++++++++++++++ src/main/resources/days/Problem21.txt | 1 + 4 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/mattrixwv/adventOfCode24/days/Problem21.java create mode 100644 src/main/java/com/mattrixwv/adventOfCode24/days/Problem22.java create mode 100644 src/main/resources/days/Problem21.txt diff --git a/src/main/java/com/mattrixwv/adventOfCode24/ProblemSelector.java b/src/main/java/com/mattrixwv/adventOfCode24/ProblemSelector.java index 44221d6..f5e2690 100644 --- a/src/main/java/com/mattrixwv/adventOfCode24/ProblemSelector.java +++ b/src/main/java/com/mattrixwv/adventOfCode24/ProblemSelector.java @@ -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 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; diff --git a/src/main/java/com/mattrixwv/adventOfCode24/days/Problem21.java b/src/main/java/com/mattrixwv/adventOfCode24/days/Problem21.java new file mode 100644 index 0000000..6145bc6 --- /dev/null +++ b/src/main/java/com/mattrixwv/adventOfCode24/days/Problem21.java @@ -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 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 performIteration(List stoneLine){ + List 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. +*/ diff --git a/src/main/java/com/mattrixwv/adventOfCode24/days/Problem22.java b/src/main/java/com/mattrixwv/adventOfCode24/days/Problem22.java new file mode 100644 index 0000000..87e6771 --- /dev/null +++ b/src/main/java/com/mattrixwv/adventOfCode24/days/Problem22.java @@ -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 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 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 performIteration(Map stoneLine){ + Map newStoneLine = new HashMap<>(); + for(Entry 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. +*/ diff --git a/src/main/resources/days/Problem21.txt b/src/main/resources/days/Problem21.txt new file mode 100644 index 0000000..6b5d2e3 --- /dev/null +++ b/src/main/resources/days/Problem21.txt @@ -0,0 +1 @@ +475449 2599064 213 0 2 65 5755 51149