From d6c9de4f71c00304e3b2e66f5b1b5eab473a7b06 Mon Sep 17 00:00:00 2001 From: gabe Date: Sat, 3 Dec 2022 02:03:38 -0600 Subject: [PATCH] made finding char score slightly faster. --- src/day03/utilities.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/day03/utilities.rs b/src/day03/utilities.rs index d5196f3..e192d61 100644 --- a/src/day03/utilities.rs +++ b/src/day03/utilities.rs @@ -13,21 +13,35 @@ pub fn parse(input: &str) -> Vec { } pub fn find_char_score(input: &char) -> usize { - static CHARS: [char; 52] = [ - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', - 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', - 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - ]; - CHARS - .iter() - .position(|character| character == input) - .unwrap()+1 + if input.is_uppercase() { + *input as usize - 38 + } else { + *input as usize - 96 + } } #[cfg(test)] mod tests { use super::*; + #[test] + fn test_find_char_score() { + static CHARS: [char; 52] = [ + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', + 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', + 'Z', + ]; + + for character in CHARS { + println!("{}", character); + assert_eq!( + find_char_score(&character), + CHARS.iter().position(|c| *c == character).unwrap() + 1 + ) + } + } + #[test] fn test_parse() { let input = "vJrwpWtwJgWrhcsFMMfFFhFp