refactored day3 to reduce unneeded mem allocations.

ran rustfmt on all files.
This commit is contained in:
gabe 2022-12-03 02:26:42 -06:00
parent 6c35a97904
commit 05d08c134b
4 changed files with 15 additions and 32 deletions

View file

@ -11,15 +11,14 @@ pub struct Strategy {
pub you: Play,
}
pub fn calc_score(input: &Strategy) -> usize{
pub fn calc_score(input: &Strategy) -> usize {
//an enum wins if (you-elf)%3 = 1, looses if it = 2
(match (input.you as i8 - input.elf as i8).rem_euclid(3) {
1 => 6,
2 => 0,
0 => 3,
_ => unreachable!("you were {}, elf was {}", input.you as i8, input.elf as i8)
//play enum has value corresponding to its score.
})+input.you as usize
_ => unreachable!("you were {}, elf was {}", input.you as i8, input.elf as i8),
}) + input.you as usize //play enum has value corresponding to its score.
}
#[cfg(test)]
@ -28,10 +27,10 @@ mod tests {
#[test]
fn test_calc_score() {
let input = Strategy {
elf: Play::Scissors,
you: Play::Scissors
};
let input = Strategy {
elf: Play::Scissors,
you: Play::Scissors,
};
assert_eq!(calc_score(&input), 6);
}
}