Skip to content

Commit 94eb8b7

Browse files
committed
Minor tidy
1 parent 25ebadb commit 94eb8b7

File tree

2 files changed

+12
-20
lines changed

2 files changed

+12
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
7878
| 4 | [Ceres Search](https://adventofcode.com/2024/day/4) | [Source](src/year2024/day04.rs) | 77 |
7979
| 5 | [Print Queue](https://adventofcode.com/2024/day/5) | [Source](src/year2024/day05.rs) | 18 |
8080
| 6 | [Guard Gallivant](https://adventofcode.com/2024/day/6) | [Source](src/year2024/day06.rs) | 386 |
81-
| 7 | [Bridge Repair](https://adventofcode.com/2024/day/7) | [Source](src/year2024/day07.rs) | 220 |
81+
| 7 | [Bridge Repair](https://adventofcode.com/2024/day/7) | [Source](src/year2024/day07.rs) | 147 |
8282

8383
## 2023
8484

src/year2024/day07.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,9 @@
3131
//! ```none
3232
//! Test Value: 2
3333
//! Equation: 2
34-
//! Addition is possible
35-
//! Multiplication is possible
36-
//! Concatenation is possible
3734
//! ```
3835
//!
39-
//! Following the addition or concatentation branches results in a test value of 0 which means
40-
//! that all terms have been applied successfully and the equation is valid.
36+
//! The test value is equal to the last term which means that the equation is valid.
4137
//!
4238
//! Inverse concenation can be implemented without time consuming conversion to or from
4339
//! strings by dividing the left term by the next power of ten greater than the right term.
@@ -81,21 +77,17 @@ pub fn part2(input: &Input) -> u64 {
8177
}
8278

8379
fn valid(terms: &[u64], test_value: u64, index: usize, concat: bool) -> bool {
84-
if test_value == 0 {
85-
return index == 0;
80+
if index == 1 {
81+
test_value == terms[1]
82+
} else {
83+
(concat
84+
&& test_value % next_power_of_ten(terms[index]) == terms[index]
85+
&& valid(terms, test_value / next_power_of_ten(terms[index]), index - 1, concat))
86+
|| (test_value % terms[index] == 0
87+
&& valid(terms, test_value / terms[index], index - 1, concat))
88+
|| (test_value >= terms[index]
89+
&& valid(terms, test_value - terms[index], index - 1, concat))
8690
}
87-
88-
if index == 0 {
89-
return false;
90-
}
91-
92-
(concat
93-
&& test_value % next_power_of_ten(terms[index]) == terms[index]
94-
&& valid(terms, test_value / next_power_of_ten(terms[index]), index - 1, concat))
95-
|| (test_value % terms[index] == 0
96-
&& valid(terms, test_value / terms[index], index - 1, concat))
97-
|| (test_value >= terms[index]
98-
&& valid(terms, test_value - terms[index], index - 1, concat))
9991
}
10092

10193
fn next_power_of_ten(n: u64) -> u64 {

0 commit comments

Comments
 (0)