Skip to content

Commit 36fcf53

Browse files
committed
Faster strategy for day 7
1 parent 4d3b9c7 commit 36fcf53

File tree

2 files changed

+35
-46
lines changed

2 files changed

+35
-46
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
1111

1212
| Day | Part 1 | Part 2 |
1313
| :---: | :---: | :---: |
14-
| [Day 1](./src/bin/01.rs) | `31.7µs` | `39.7µs` |
15-
| [Day 2](./src/bin/02.rs) | `46.5µs` | `67.4µs` |
16-
| [Day 3](./src/bin/03.rs) | `213.3µs` | `238.4µs` |
17-
| [Day 4](./src/bin/04.rs) | `148.6µs` | `33.1µs` |
18-
| [Day 5](./src/bin/05.rs) | `65.6µs` | `181.1µs` |
19-
| [Day 6](./src/bin/06.rs) | `44.6µs` | `12.3ms` |
20-
| [Day 7](./src/bin/07.rs) | `554.7µs` | `7.1ms` |
21-
22-
**Total: 21.06ms**
14+
| [Day 1](./src/bin/01.rs) | `31.6µs` | `40.7µs` |
15+
| [Day 2](./src/bin/02.rs) | `47.2µs` | `68.6µs` |
16+
| [Day 3](./src/bin/03.rs) | `222.9µs` | `246.5µs` |
17+
| [Day 4](./src/bin/04.rs) | `145.9µs` | `33.2µs` |
18+
| [Day 5](./src/bin/05.rs) | `66.0µs` | `188.7µs` |
19+
| [Day 6](./src/bin/06.rs) | `44.8µs` | `11.1ms` |
20+
| [Day 7](./src/bin/07.rs) | `316.2µs` | `319.6µs` |
21+
22+
**Total: 12.87ms**
2323
<!--- benchmarking table --->
2424

2525
---

src/bin/07.rs

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use heapless::Vec as HeaplessVec;
21
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
32
advent_of_code::solution!(7);
43

@@ -25,58 +24,48 @@ impl EquationData {
2524

2625
#[inline]
2726
fn has_valid_ops_combination(&self, available_ops: &[Op]) -> bool {
28-
let needed_ops = self.values.len() - 1;
29-
if needed_ops == 0 {
30-
return self.values[0] == self.test_value;
31-
}
32-
33-
const OPS_SIZE: usize = 16;
34-
const STACK_SIZE: usize = 32;
35-
36-
let mut stack = HeaplessVec::<_, STACK_SIZE>::new();
37-
stack
38-
.push((self.values[0], HeaplessVec::<_, OPS_SIZE>::new()))
39-
.unwrap();
27+
// Stack will store (current_target, remaining_numbers_index);
28+
let mut stack = vec![(self.test_value, self.values.len() - 1)];
4029

41-
while let Some((current_value, mut ops)) = stack.pop() {
42-
if ops.len() == needed_ops {
43-
if current_value == self.test_value {
30+
while let Some((current_target, idx)) = stack.pop() {
31+
// Base case - if we're at the first number
32+
if idx == 0 {
33+
if self.values[0] == current_target {
4434
return true;
4535
}
4636
continue;
4737
}
4838

49-
let next_idx = ops.len() + 1;
50-
let next_value = self.values[next_idx];
39+
let n = self.values[idx];
5140

52-
// Try each available operation
5341
for &op in available_ops {
54-
let new_value = match op {
55-
Op::Add => current_value + next_value,
56-
Op::Mul => current_value * next_value,
42+
match op {
43+
Op::Add => {
44+
if current_target >= n {
45+
stack.push((current_target - n, idx - 1));
46+
}
47+
}
48+
Op::Mul => {
49+
if current_target % n == 0 {
50+
stack.push((current_target / n, idx - 1));
51+
}
52+
}
5753
Op::Concat => {
5854
let mut digit_count = 0;
59-
let mut n = next_value;
60-
while n > 0 {
55+
let mut temp = n;
56+
while temp > 0 {
6157
digit_count += 1;
62-
n /= 10;
58+
temp /= 10;
6359
}
64-
current_value * 10_u64.pow(digit_count) + next_value
65-
}
66-
};
60+
let divisor = 10_u64.pow(digit_count);
6761

68-
// Skip if we've already exceeded the target
69-
if new_value > self.test_value {
70-
continue;
62+
if current_target % divisor == n {
63+
stack.push((current_target / divisor, idx - 1));
64+
}
65+
}
7166
}
72-
73-
// Create new ops vector and push to stack
74-
ops.push(op).unwrap();
75-
stack.push((new_value, ops.clone())).unwrap();
76-
ops.pop();
7767
}
7868
}
79-
8069
false
8170
}
8271
}

0 commit comments

Comments
 (0)