Skip to content

Commit

Permalink
feat: solve day 7
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 7, 2024
1 parent c808ec0 commit e294201
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/adventofcode/flashk/day07/BridgeRepair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.adventofcode.flashk.day07;

import java.util.List;

public class BridgeRepair {

private final List<Equation> equations;

public BridgeRepair(List<String> inputs) {
equations = inputs.stream().map(Equation::new).toList();
}

public long solve(boolean concatenate) {
return equations.stream().mapToLong(eq -> eq.solve(concatenate)).sum();
}

}
60 changes: 60 additions & 0 deletions src/main/java/com/adventofcode/flashk/day07/Equation.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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<Integer> 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;
}
}

2 changes: 1 addition & 1 deletion src/main/java/com/adventofcode/flashk/day07/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Day 7:
# Day 7: Bridge Repair

[https://adventofcode.com/2024/day/7](https://adventofcode.com/2024/day/7)
20 changes: 15 additions & 5 deletions src/test/java/com/adventofcode/flashk/day07/Day07Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -43,6 +43,9 @@ public void testSolvePart1Sample() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);

BridgeRepair bridgeRepair = new BridgeRepair(inputs);
assertEquals(3749, bridgeRepair.solve(false));

}

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

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);

BridgeRepair bridgeRepair = new BridgeRepair(inputs);

assertEquals(3351424677624L, bridgeRepair.solve(false));
}

@Test
Expand All @@ -71,7 +76,10 @@ public void testSolvePart2Sample() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);

BridgeRepair bridgeRepair = new BridgeRepair(inputs);

assertEquals(11387, bridgeRepair.solve(true));

}

@Test
Expand All @@ -85,7 +93,9 @@ public void testSolvePart2Input() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);

BridgeRepair bridgeRepair = new BridgeRepair(inputs);

assertEquals(204976636995111L, bridgeRepair.solve(true));
}

}
2 changes: 1 addition & 1 deletion src/test/resources/inputs

0 comments on commit e294201

Please sign in to comment.