Skip to content

Commit 5e2972d

Browse files
committed
Optimize 2024 day 17
1 parent e685d36 commit 5e2972d

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

crates/year2024/src/day17.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,17 @@ impl Day17 {
3434

3535
#[must_use]
3636
pub fn part1(&self) -> String {
37-
let mut output = Vec::new();
37+
let mut output = String::new();
3838

3939
self.run(self.a_start, |x| {
40-
output.push(x);
40+
if !output.is_empty() {
41+
output.push(',');
42+
}
43+
output.push((b'0' + x) as char);
4144
ControlFlow::Continue(())
4245
});
4346

4447
output
45-
.iter()
46-
.map(|n| n.to_string())
47-
.collect::<Vec<_>>()
48-
.join(",")
4948
}
5049

5150
#[inline]
@@ -122,16 +121,10 @@ impl Day17 {
122121
}
123122

124123
fn check_pos_matches(&self, a: u64, pos: usize) -> bool {
125-
let mut count = 0;
126124
let mut output = None;
127-
self.run(a, |out| {
128-
if count == pos {
129-
output = Some(out);
130-
ControlFlow::Break(())
131-
} else {
132-
count += 1;
133-
ControlFlow::Continue(())
134-
}
125+
self.run(a / (1 << (pos * 3)), |out| {
126+
output = Some(out);
127+
ControlFlow::Break(())
135128
});
136129
output == Some(self.program[pos])
137130
}

0 commit comments

Comments
 (0)