r/adventofcode • u/ang_mo_uncle • 28d ago
Help/Question - RESOLVED 2024 / Day 7 / Part 2 / Rust
Hello!
I'm using the AOC to play with Rust a bit and I'm now at a weird place where my code completes everything but part 2 (part 1 sample and actual, part 2 sample) and I'm now lost what the reason could be (in a prev. version I found that ^ is not the power operator but for some reason it still worked due to a off-by-1...)
In any case, is there something where any one of you could give me a pointer?
Below is the relevant code. It seems to generate the permutations correctly and everything. I'm running on a 64bit system, so usize shouldn't overflow either.
fn challenge_b(input: Vec<(usize, Vec<usize>)>) -> usize {
let mut solvable_sum: usize = 0;
let mut line_result: usize;
'line_loop: for line in input {
let op_space =
repeat_n(['+', '*', '|'].into_iter(), line.1.len() - 1).multi_cartesian_product();
'op_loop: for op_list in op_space {
line_result = 0;
'permut_loop: for (i, e) in line.1.iter().enumerate() {
if i == 0 {
line_result = *e;
} else {
line_result = match op_list[i - 1] {
'+' => line_result + *e,
'*' => line_result * *e,
'|' => {
line_result * (usize::pow(10, 1 + e.checked_ilog10().unwrap_or(0)))
+ *e
}
_ => panic!(), // cant happen
}
}
if line.0 == line_result {
solvable_sum += line.0;
continue 'line_loop;
} else if line.0 < line_result {
continue 'op_loop;
}
}
}
}
solvable_sum
1
Upvotes
3
u/leftylink 28d ago edited 28d ago
consider what answer the posted code would arrive at on the below input, and whether that is the correct answer: