Skip to content

Commit e294201

Browse files
committed
feat: solve day 7
1 parent c808ec0 commit e294201

File tree

6 files changed

+95
-8
lines changed

6 files changed

+95
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
- [Day 4 - Ceres Search](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day04)
1010
- [Day 5 - Print Queue](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day05)
1111
- [Day 6 - Guard Gallivant](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day06)
12-
- [Day 7](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day07)
12+
- [Day 7 - Bridge Repair](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day07)
1313
- [Day 8](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day08)
1414
- [Day 9](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day09)
1515
- [Day 10](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day10)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.adventofcode.flashk.day07;
2+
3+
import java.util.List;
4+
5+
public class BridgeRepair {
6+
7+
private final List<Equation> equations;
8+
9+
public BridgeRepair(List<String> inputs) {
10+
equations = inputs.stream().map(Equation::new).toList();
11+
}
12+
13+
public long solve(boolean concatenate) {
14+
return equations.stream().mapToLong(eq -> eq.solve(concatenate)).sum();
15+
}
16+
17+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.adventofcode.flashk.day07;
2+
3+
import lombok.Getter;
4+
5+
import java.util.Arrays;
6+
import java.util.Deque;
7+
import java.util.ArrayDeque;
8+
import java.util.stream.Collectors;
9+
10+
public class Equation {
11+
12+
private final long result;
13+
private final Deque<Integer> operators;
14+
private boolean concatenate;
15+
16+
public Equation(String input) {
17+
String[] inputParts = input.split(":");
18+
result = Long.parseLong(inputParts[0]);
19+
operators = Arrays.stream(inputParts[1].split(" "))
20+
.skip(1).map(Integer::parseInt).collect(Collectors.toCollection(ArrayDeque::new));
21+
}
22+
23+
public long solve(boolean concatenate) {
24+
this.concatenate = concatenate;
25+
return hasSolution(operators, 0) ? result : 0;
26+
}
27+
28+
private boolean hasSolution(Deque<Integer> operators, long partialResult) {
29+
30+
if(operators.isEmpty()) {
31+
return partialResult == result;
32+
}
33+
34+
Integer currentOperator = operators.poll();
35+
boolean hasSolution = hasSolution(operators, partialResult + currentOperator);
36+
37+
if(!hasSolution && partialResult != 0) {
38+
hasSolution = hasSolution(operators, partialResult * currentOperator);
39+
} else if(partialResult == 0) {
40+
hasSolution = hasSolution(operators, currentOperator);
41+
}
42+
43+
if(!hasSolution && concatenate) {
44+
StringBuilder sb = new StringBuilder();
45+
46+
if(partialResult != 0) {
47+
sb.append(partialResult);
48+
}
49+
50+
long concatenatedPartialResult = Long.parseLong(sb.append(currentOperator).toString());
51+
hasSolution = hasSolution(operators, concatenatedPartialResult);
52+
53+
}
54+
55+
operators.addFirst(currentOperator);
56+
57+
return hasSolution;
58+
}
59+
}
60+
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Day 7:
1+
# Day 7: Bridge Repair
22

33
[https://adventofcode.com/2024/day/7](https://adventofcode.com/2024/day/7)

src/test/java/com/adventofcode/flashk/day07/Day07Test.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.List;
44

55
import org.junit.jupiter.api.BeforeAll;
6-
import org.junit.jupiter.api.Disabled;
76
import org.junit.jupiter.api.DisplayName;
87
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
98
import org.junit.jupiter.api.Order;
@@ -19,9 +18,10 @@
1918
import com.adventofcode.flashk.common.test.utils.Timer;
2019
import com.adventofcode.flashk.common.test.utils.Input;
2120

21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
2223
@DisplayName(TestDisplayName.DAY_07)
2324
@TestMethodOrder(OrderAnnotation.class)
24-
@Disabled // TODO Remove comment when implemented
2525
public class Day07Test extends PuzzleTest {
2626

2727
private final static String INPUT_FOLDER = TestFolder.DAY_07;
@@ -43,6 +43,9 @@ public void testSolvePart1Sample() {
4343

4444
// Read input file
4545
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
46+
47+
BridgeRepair bridgeRepair = new BridgeRepair(inputs);
48+
assertEquals(3749, bridgeRepair.solve(false));
4649

4750
}
4851

@@ -57,7 +60,9 @@ public void testSolvePart1Input() {
5760

5861
// Read input file
5962
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
60-
63+
BridgeRepair bridgeRepair = new BridgeRepair(inputs);
64+
65+
assertEquals(3351424677624L, bridgeRepair.solve(false));
6166
}
6267

6368
@Test
@@ -71,7 +76,10 @@ public void testSolvePart2Sample() {
7176

7277
// Read input file
7378
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
74-
79+
BridgeRepair bridgeRepair = new BridgeRepair(inputs);
80+
81+
assertEquals(11387, bridgeRepair.solve(true));
82+
7583
}
7684

7785
@Test
@@ -85,7 +93,9 @@ public void testSolvePart2Input() {
8593

8694
// Read input file
8795
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
88-
96+
BridgeRepair bridgeRepair = new BridgeRepair(inputs);
97+
98+
assertEquals(204976636995111L, bridgeRepair.solve(true));
8999
}
90100

91101
}

src/test/resources/inputs

0 commit comments

Comments
 (0)