Skip to content

Commit

Permalink
refactor: use same algorithm for day 11 parts 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 11, 2024
1 parent f8d185b commit 8244698
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 77 deletions.
52 changes: 8 additions & 44 deletions src/main/java/com/adventofcode/flashk/day11/PlutonianPebbles.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,24 @@

public class PlutonianPebbles {

private Deque<Long> rocks;

private Map<Long, Long> rocksPerNumber;

public PlutonianPebbles(List<String> 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<Long,Long> processedRocks = new HashMap<>();

Expand Down Expand Up @@ -73,40 +65,12 @@ private void blinkB() {
rocksPerNumber = processedRocks;
}

private long countRocksInMap() {
private long countRocks() {
long result = 0;
for(Long number : rocksPerNumber.keySet()) {
result += rocksPerNumber.get(number);
}
return result;
}

private void blink() {

Deque<Long> 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;
}
}
41 changes: 8 additions & 33 deletions src/test/java/com/adventofcode/flashk/day11/Day11Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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<String> 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));

}

Expand All @@ -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<String> 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<String> 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));

}

Expand All @@ -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<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);

PlutonianPebbles plutonianPebbles = new PlutonianPebbles(inputs);

assertEquals(224577979481346L, plutonianPebbles.solveB(75));
assertEquals(224577979481346L, plutonianPebbles.solve(75));

}

Expand Down

0 comments on commit 8244698

Please sign in to comment.