From 82446984603aa6896096219f1501fd33b3c88bca Mon Sep 17 00:00:00 2001 From: Flashky Date: Wed, 11 Dec 2024 08:21:01 +0100 Subject: [PATCH] refactor: use same algorithm for day 11 parts 1 and 2 --- .../flashk/day11/PlutonianPebbles.java | 52 +++---------------- .../adventofcode/flashk/day11/Day11Test.java | 41 +++------------ 2 files changed, 16 insertions(+), 77 deletions(-) diff --git a/src/main/java/com/adventofcode/flashk/day11/PlutonianPebbles.java b/src/main/java/com/adventofcode/flashk/day11/PlutonianPebbles.java index c0361c8..e991f93 100644 --- a/src/main/java/com/adventofcode/flashk/day11/PlutonianPebbles.java +++ b/src/main/java/com/adventofcode/flashk/day11/PlutonianPebbles.java @@ -13,32 +13,24 @@ public class PlutonianPebbles { - private Deque rocks; - private Map rocksPerNumber; public PlutonianPebbles(List inputs) { - rocks = Arrays.stream(inputs.get(0).split(StringUtils.SPACE)).map(Long::valueOf).collect(Collectors.toCollection(ArrayDeque::new)); - - rocksPerNumber = rocks.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); + rocksPerNumber = Arrays.stream(inputs.get(0).split(StringUtils.SPACE)).map(Long::valueOf) + .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); } - public long solveA(int blinks) { - for(int i = 0; i < blinks; i++) { - blink(); - } - return rocks.size(); - } + public long solve(int blinks) { - public long solveB(int blinks) { for(int i = 0; i < blinks; i++) { - blinkB(); + blink(); } - return countRocksInMap(); + + return countRocks(); } - private void blinkB() { + private void blink() { Map processedRocks = new HashMap<>(); @@ -73,7 +65,7 @@ private void blinkB() { rocksPerNumber = processedRocks; } - private long countRocksInMap() { + private long countRocks() { long result = 0; for(Long number : rocksPerNumber.keySet()) { result += rocksPerNumber.get(number); @@ -81,32 +73,4 @@ private long countRocksInMap() { return result; } - private void blink() { - - Deque processedRocks = new ArrayDeque<>(); - - // Para cada roca: - // Si roca == 0 -> roca = 1 - // y si roca tiene un número par de dígitos -> Partir en 2 -> tendré que convertir a string y partir. - // resto de casos: roca *= 2024 - - while(!rocks.isEmpty()) { - long number = rocks.pollFirst(); - long digits = String.valueOf(number).length(); - if(number == 0) { - processedRocks.add(1L); - } else if(digits % 2 == 0) { - String strNumber = String.valueOf(number); - String numberLeft = strNumber.substring(0, strNumber.length() / 2); - String numberRight = strNumber.substring(strNumber.length() / 2); - processedRocks.add(Long.valueOf(numberLeft)); - processedRocks.add(Long.valueOf(numberRight)); - } else { - processedRocks.add(number*2024); - } - } - - - rocks = processedRocks; - } } diff --git a/src/test/java/com/adventofcode/flashk/day11/Day11Test.java b/src/test/java/com/adventofcode/flashk/day11/Day11Test.java index 997e2b1..c1b994a 100644 --- a/src/test/java/com/adventofcode/flashk/day11/Day11Test.java +++ b/src/test/java/com/adventofcode/flashk/day11/Day11Test.java @@ -2,7 +2,6 @@ import java.util.List; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import org.junit.jupiter.api.Order; @@ -14,15 +13,13 @@ import com.adventofcode.flashk.common.test.constants.TestFilename; import com.adventofcode.flashk.common.test.constants.TestFolder; import com.adventofcode.flashk.common.test.constants.TestTag; -import com.adventofcode.flashk.common.test.utils.PuzzleTest; import com.adventofcode.flashk.common.test.utils.Input; import static org.junit.jupiter.api.Assertions.assertEquals; @DisplayName(TestDisplayName.DAY_11) @TestMethodOrder(OrderAnnotation.class) -@Disabled // TODO Remove comment when implemented -public class Day11Test extends PuzzleTest { +class Day11Test { private static final String INPUT_FOLDER = TestFolder.DAY_11; @@ -31,17 +28,16 @@ public class Day11Test extends PuzzleTest { @Tag(TestTag.PART_1) @Tag(TestTag.SAMPLE) @DisplayName(TestDisplayName.PART_1_SAMPLE) - public void testSolvePart1Sample() { + void part1SampleTest() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE); PlutonianPebbles plutonianPebbles = new PlutonianPebbles(inputs); - - assertEquals(22L ,plutonianPebbles.solveA(6)); + assertEquals(22L ,plutonianPebbles.solve(6)); plutonianPebbles = new PlutonianPebbles(inputs); - assertEquals(55312L ,plutonianPebbles.solveA(25)); + assertEquals(55312L ,plutonianPebbles.solve(25)); } @@ -50,33 +46,13 @@ public void testSolvePart1Sample() { @Tag(TestTag.PART_1) @Tag(TestTag.INPUT) @DisplayName(TestDisplayName.PART_1_INPUT) - public void testSolvePart1Input() { + void part1InputTest() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE); - PlutonianPebbles plutonianPebbles = new PlutonianPebbles(inputs); - - System.out.println("Solution: "+plutonianPebbles.solveA(75)); - //assertEquals(189547L,0L); - - } - - @Test - @Order(1) - @Tag(TestTag.PART_1) - @Tag(TestTag.SAMPLE) - @DisplayName(TestDisplayName.PART_2_SAMPLE) - public void testSolvePart2Sample() { - - // Read input file - List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE); PlutonianPebbles plutonianPebbles = new PlutonianPebbles(inputs); - - assertEquals(22L ,plutonianPebbles.solveB(6)); - - plutonianPebbles = new PlutonianPebbles(inputs); - assertEquals(55312L ,plutonianPebbles.solveB(25)); + assertEquals(189547L,plutonianPebbles.solve(25)); } @@ -85,14 +61,13 @@ public void testSolvePart2Sample() { @Tag(TestTag.PART_2) @Tag(TestTag.INPUT) @DisplayName(TestDisplayName.PART_2_INPUT) - public void testSolvePart2Input() { + void part2InputTest() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE); PlutonianPebbles plutonianPebbles = new PlutonianPebbles(inputs); - - assertEquals(224577979481346L, plutonianPebbles.solveB(75)); + assertEquals(224577979481346L, plutonianPebbles.solve(75)); }