Skip to content

Commit cad8919

Browse files
committed
perf(17/2024): add early returns, but still slow to slove for main input
1 parent eafec20 commit cad8919

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/solutions/year2024/day17.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@ impl Solution for Day17 {
1515
fn part_two(&self, input: &str) -> String {
1616
let (_, program) = self.parse(input);
1717

18-
let mut i = 0;
19-
loop {
18+
for i in 0.. {
2019
let mut register = RegisterBuilder::default().a(i).build();
2120

2221
if program.execute_and_watch(&mut register) {
2322
return i.to_string();
2423
}
2524

26-
i += 1;
25+
println!("{}", i);
2726
}
27+
28+
unreachable!()
2829
}
2930
}
3031

@@ -113,9 +114,21 @@ impl Program {
113114
&mut output,
114115
);
115116

117+
if output.is_empty() {
118+
continue;
119+
}
120+
116121
if expected == output {
117122
return true;
118123
}
124+
125+
if expected.len() <= output.len() {
126+
return false;
127+
}
128+
129+
if expected[0..output.len()] != output {
130+
return false;
131+
}
119132
}
120133

121134
false

0 commit comments

Comments
 (0)