diff --git a/README.md b/README.md index 5570b68..8945e42 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ - [Day 4 - Ceres Search](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day04) - [Day 5 - Print Queue](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day05) - [Day 6 - Guard Gallivant](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day06) -- [Day 7](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day07) +- [Day 7 - Bridge Repair](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day07) - [Day 8](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day08) - [Day 9](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day09) - [Day 10](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day10) diff --git a/src/main/java/com/adventofcode/flashk/day07/BridgeRepair.java b/src/main/java/com/adventofcode/flashk/day07/BridgeRepair.java new file mode 100644 index 0000000..ba46e96 --- /dev/null +++ b/src/main/java/com/adventofcode/flashk/day07/BridgeRepair.java @@ -0,0 +1,17 @@ +package com.adventofcode.flashk.day07; + +import java.util.List; + +public class BridgeRepair { + + private final List equations; + + public BridgeRepair(List inputs) { + equations = inputs.stream().map(Equation::new).toList(); + } + + public long solve(boolean concatenate) { + return equations.stream().mapToLong(eq -> eq.solve(concatenate)).sum(); + } + +} diff --git a/src/main/java/com/adventofcode/flashk/day07/Equation.java b/src/main/java/com/adventofcode/flashk/day07/Equation.java new file mode 100644 index 0000000..62acf80 --- /dev/null +++ b/src/main/java/com/adventofcode/flashk/day07/Equation.java @@ -0,0 +1,60 @@ +package com.adventofcode.flashk.day07; + +import lombok.Getter; + +import java.util.Arrays; +import java.util.Deque; +import java.util.ArrayDeque; +import java.util.stream.Collectors; + +public class Equation { + + private final long result; + private final Deque operators; + private boolean concatenate; + + public Equation(String input) { + String[] inputParts = input.split(":"); + result = Long.parseLong(inputParts[0]); + operators = Arrays.stream(inputParts[1].split(" ")) + .skip(1).map(Integer::parseInt).collect(Collectors.toCollection(ArrayDeque::new)); + } + + public long solve(boolean concatenate) { + this.concatenate = concatenate; + return hasSolution(operators, 0) ? result : 0; + } + + private boolean hasSolution(Deque operators, long partialResult) { + + if(operators.isEmpty()) { + return partialResult == result; + } + + Integer currentOperator = operators.poll(); + boolean hasSolution = hasSolution(operators, partialResult + currentOperator); + + if(!hasSolution && partialResult != 0) { + hasSolution = hasSolution(operators, partialResult * currentOperator); + } else if(partialResult == 0) { + hasSolution = hasSolution(operators, currentOperator); + } + + if(!hasSolution && concatenate) { + StringBuilder sb = new StringBuilder(); + + if(partialResult != 0) { + sb.append(partialResult); + } + + long concatenatedPartialResult = Long.parseLong(sb.append(currentOperator).toString()); + hasSolution = hasSolution(operators, concatenatedPartialResult); + + } + + operators.addFirst(currentOperator); + + return hasSolution; + } +} + diff --git a/src/main/java/com/adventofcode/flashk/day07/README.md b/src/main/java/com/adventofcode/flashk/day07/README.md index 793f055..ace9166 100644 --- a/src/main/java/com/adventofcode/flashk/day07/README.md +++ b/src/main/java/com/adventofcode/flashk/day07/README.md @@ -1,3 +1,3 @@ -# Day 7: +# Day 7: Bridge Repair [https://adventofcode.com/2024/day/7](https://adventofcode.com/2024/day/7) diff --git a/src/test/java/com/adventofcode/flashk/day07/Day07Test.java b/src/test/java/com/adventofcode/flashk/day07/Day07Test.java index 565ab4c..a216a53 100644 --- a/src/test/java/com/adventofcode/flashk/day07/Day07Test.java +++ b/src/test/java/com/adventofcode/flashk/day07/Day07Test.java @@ -3,7 +3,6 @@ import java.util.List; import org.junit.jupiter.api.BeforeAll; -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; @@ -19,9 +18,10 @@ import com.adventofcode.flashk.common.test.utils.Timer; import com.adventofcode.flashk.common.test.utils.Input; +import static org.junit.jupiter.api.Assertions.assertEquals; + @DisplayName(TestDisplayName.DAY_07) @TestMethodOrder(OrderAnnotation.class) -@Disabled // TODO Remove comment when implemented public class Day07Test extends PuzzleTest { private final static String INPUT_FOLDER = TestFolder.DAY_07; @@ -43,6 +43,9 @@ public void testSolvePart1Sample() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE); + + BridgeRepair bridgeRepair = new BridgeRepair(inputs); + assertEquals(3749, bridgeRepair.solve(false)); } @@ -57,7 +60,9 @@ public void testSolvePart1Input() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE); - + BridgeRepair bridgeRepair = new BridgeRepair(inputs); + + assertEquals(3351424677624L, bridgeRepair.solve(false)); } @Test @@ -71,7 +76,10 @@ public void testSolvePart2Sample() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE); - + BridgeRepair bridgeRepair = new BridgeRepair(inputs); + + assertEquals(11387, bridgeRepair.solve(true)); + } @Test @@ -85,7 +93,9 @@ public void testSolvePart2Input() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE); - + BridgeRepair bridgeRepair = new BridgeRepair(inputs); + + assertEquals(204976636995111L, bridgeRepair.solve(true)); } } diff --git a/src/test/resources/inputs b/src/test/resources/inputs index 490a6c0..93683de 160000 --- a/src/test/resources/inputs +++ b/src/test/resources/inputs @@ -1 +1 @@ -Subproject commit 490a6c0ad8ea83d5dacab5a3b085d4140e06c762 +Subproject commit 93683de81689415e303832e75b1169201d709640