Skip to content

Commit

Permalink
2023: Add performance hack for day 3
Browse files Browse the repository at this point in the history
Not a good idea for all kinds of strings.
  • Loading branch information
jp7677 committed Dec 4, 2023
1 parent 42074f2 commit 5ee921a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion 2023/src/day03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fn parse_map(input: &String) -> Vec<Part> {
})
.collect::<Vec<Adjacent>>();

if i >= 1 && input.chars().nth(i - 1).unwrap_or('_').is_digit(10) {
if i >= 1 && char_at(input, i - 1).is_digit(10) {
parts.last_mut().unwrap().number.push(c);
parts
.last_mut()
Expand Down
2 changes: 2 additions & 0 deletions 2023/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod days;
mod input;
mod strings;

pub use days::DAYS;
pub use input::read_input;
pub use strings::char_at;
5 changes: 5 additions & 0 deletions 2023/src/util/strings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[inline(always)]
pub fn char_at(str: &str, index: usize) -> char {
// Not a good idea for all kinds of strings.
*(&str[index..index + 1].chars().nth(0).unwrap())
}

0 comments on commit 5ee921a

Please sign in to comment.