made finding char score slightly faster.

This commit is contained in:
gabe 2022-12-03 02:03:38 -06:00
parent 5d22d7975a
commit d6c9de4f71

View file

@ -13,21 +13,35 @@ pub fn parse(input: &str) -> Vec<Rucksack> {
} }
pub fn find_char_score(input: &char) -> usize { pub fn find_char_score(input: &char) -> usize {
static CHARS: [char; 52] = [ if input.is_uppercase() {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', *input as usize - 38
's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', } else {
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', *input as usize - 96
]; }
CHARS
.iter()
.position(|character| character == input)
.unwrap()+1
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; 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] #[test]
fn test_parse() { fn test_parse() {
let input = "vJrwpWtwJgWrhcsFMMfFFhFp let input = "vJrwpWtwJgWrhcsFMMfFFhFp