From 5a4a7d256cc52663b167256cad60425abb11b4f3 Mon Sep 17 00:00:00 2001 From: TheCK Date: Wed, 11 Dec 2024 06:24:48 +0100 Subject: [PATCH] aoc 2024 day 11 --- adventofcode/README.md | 6 +- .../org/ck/adventofcode/year2024/Day11.java | 63 +++++++++++++++---- .../ck/adventofcode/year2024/Day11Test.java | 4 +- .../adventofcode/year2024/day11/01.result.txt | 1 + .../org/ck/adventofcode/year2024/day11/01.txt | 1 + .../year2024/day11/01a.result.txt | 1 + .../ck/adventofcode/year2024/day11/01a.txt | 1 + .../adventofcode/year2024/day11/02.result.txt | 1 + .../org/ck/adventofcode/year2024/day11/02.txt | 1 + .../year2024/day11/02a.result.txt | 0 .../ck/adventofcode/year2024/day11/02a.txt | 0 11 files changed, 62 insertions(+), 17 deletions(-) delete mode 100644 adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/02a.result.txt delete mode 100644 adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/02a.txt diff --git a/adventofcode/README.md b/adventofcode/README.md index 650d78a0..11d10161 100644 --- a/adventofcode/README.md +++ b/adventofcode/README.md @@ -1422,7 +1422,7 @@ [20232402tests]: src/test/java/org/ck/adventofcode/year2023/Day24Test.java [20232501tests]: src/test/java/org/ck/adventofcode/year2023/Day25Test.java -# 2024 (20/49) +# 2024 (22/49) | # | Name | Solution | Test | |---------:|---------------------------------------------------|:------------------------------------:|:---------------------------------:| @@ -1446,8 +1446,8 @@ | 20240902 | [Day 9: Disk Fragmenter - Part 2][20240902] | ✅[💾][20240902solution] | ✅[💾][20240902tests] | | 20241001 | [Day 10: Hoof It][20241001] | ✅[💾][20241001solution] | ✅[💾][20241001tests] | | 20241002 | [Day 10: Hoof It - Part 2][20241002] | ✅[💾][20241002solution] | ✅[💾][20241002tests] | -| 20241101 | [Day 11: ?][20241101] | [💾][20241101solution] | [💾][20241101tests] | -| 20241102 | [Day 11: ? - Part 2][20241102] | [💾][20241102solution] | [💾][20241102tests] | +| 20241101 | [Day 11: Plutonian Pebbles][20241101] | ✅[💾][20241101solution] | ✅[💾][20241101tests] | +| 20241102 | [Day 11: Plutonian Pebbles - Part 2][20241102] | ✅[💾][20241102solution] | ✅[💾][20241102tests] | | 20241201 | [Day 12: ?][20241201] | [💾][20241201solution] | [💾][20241201tests] | | 20241202 | [Day 12: ? - Part 2][20241202] | [💾][20241202solution] | [💾][20241202tests] | | 20241301 | [Day 13: ?][20241301] | [💾][20241301solution] | [💾][20241301tests] | diff --git a/adventofcode/src/main/java/org/ck/adventofcode/year2024/Day11.java b/adventofcode/src/main/java/org/ck/adventofcode/year2024/Day11.java index 49f124e8..e503647d 100644 --- a/adventofcode/src/main/java/org/ck/adventofcode/year2024/Day11.java +++ b/adventofcode/src/main/java/org/ck/adventofcode/year2024/Day11.java @@ -1,34 +1,75 @@ package org.ck.adventofcode.year2024; -import java.util.Scanner; +import java.util.*; import org.ck.adventofcode.util.AOCSolution; import org.ck.codechallengelib.annotation.Solution; @Solution( id = 20241101, - name = "Day 11: ?", + name = "Day 11: Plutonian Pebbles", url = "https://adventofcode.com/2024/day/11", - category = "2024", - solved = false) + category = "2024") @Solution( id = 20241102, - name = "Day 11: ? - Part 2", + name = "Day 11: Plutonian Pebbles - Part 2", url = "https://adventofcode.com/2024/day/11#part2", - category = "2024", - solved = false) + category = "2024") public class Day11 extends AOCSolution { @Override protected void runPartOne(final Scanner in) { - run(in); + run(in, 25); } @Override protected void runPartTwo(final Scanner in) { - run(in); + run(in, 75); } - private void run(final Scanner in) { - print("Whoopsie"); + private void run(final Scanner in, final int steps) { + final List numbers = + Arrays.stream(in.nextLine().split(" ")).map(Long::parseLong).toList(); + + final Map cache = new HashMap<>(); + long sum = 0; + + for (Long number : numbers) { + sum += getValueAfterSteps(cache, number, steps); + } + + print(sum); + } + + private long getValueAfterSteps( + final Map cache, final Long number, final int steps) { + final String key = "%d-%d".formatted(number, steps); + if (cache.containsKey(key)) { + return cache.get(key); + } + + if (steps == 0) { + return 1; + } + + long value = 0; + + if (number == 0) { + value += getValueAfterSteps(cache, 1L, steps - 1); + } else { + final String baseTen = String.valueOf(number); + + if (baseTen.length() % 2 == 0) { + value += + getValueAfterSteps( + cache, Long.valueOf(baseTen.substring(0, baseTen.length() / 2)), steps - 1) + + getValueAfterSteps( + cache, Long.valueOf(baseTen.substring(baseTen.length() / 2)), steps - 1); + } else { + value += getValueAfterSteps(cache, number * 2024, steps - 1); + } + } + + cache.put(key, value); + return value; } } diff --git a/adventofcode/src/test/java/org/ck/adventofcode/year2024/Day11Test.java b/adventofcode/src/test/java/org/ck/adventofcode/year2024/Day11Test.java index 7a02ab0d..40437fc5 100644 --- a/adventofcode/src/test/java/org/ck/adventofcode/year2024/Day11Test.java +++ b/adventofcode/src/test/java/org/ck/adventofcode/year2024/Day11Test.java @@ -1,11 +1,9 @@ package org.ck.adventofcode.year2024; import org.ck.adventofcode.util.BaseAOCTest; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -@Disabled class Day11Test extends BaseAOCTest { @ParameterizedTest @ValueSource(strings = {"01", "01a"}) @@ -14,7 +12,7 @@ void testOne(String name) throws Exception { } @ParameterizedTest - @ValueSource(strings = {"02", "02a"}) + @ValueSource(strings = {"02"}) void testTwo(String name) throws Exception { runTest(new Day11()::partTwo, "day11/%s".formatted(name)); } diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/01.result.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/01.result.txt index e69de29b..2278e48f 100644 --- a/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/01.result.txt +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/01.result.txt @@ -0,0 +1 @@ +222461 \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/01.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/01.txt index e69de29b..05aee0ad 100644 --- a/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/01.txt +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/01.txt @@ -0,0 +1 @@ +2 54 992917 5270417 2514 28561 0 990 \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/01a.result.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/01a.result.txt index e69de29b..70bbfd7c 100644 --- a/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/01a.result.txt +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/01a.result.txt @@ -0,0 +1 @@ +55312 \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/01a.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/01a.txt index e69de29b..528f9d50 100644 --- a/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/01a.txt +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/01a.txt @@ -0,0 +1 @@ +125 17 \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/02.result.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/02.result.txt index e69de29b..4605d5ae 100644 --- a/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/02.result.txt +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/02.result.txt @@ -0,0 +1 @@ +264350935776416 \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/02.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/02.txt index e69de29b..05aee0ad 100644 --- a/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/02.txt +++ b/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/02.txt @@ -0,0 +1 @@ +2 54 992917 5270417 2514 28561 0 990 \ No newline at end of file diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/02a.result.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/02a.result.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/02a.txt b/adventofcode/src/test/resources/org/ck/adventofcode/year2024/day11/02a.txt deleted file mode 100644 index e69de29b..00000000