generated from Flashky/advent-of-code-yyyy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
95 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/com/adventofcode/flashk/day07/BridgeRepair.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule inputs
updated
from 490a6c to 93683d