advent_of_code_2022/days/day02/src/utilities.rs
Gabe Venberg 242989bb95 switched to workspaces.
This should let me make a cross-day library.
2023-11-19 20:32:41 -06:00

36 lines
831 B
Rust

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Play {
Rock = 1,
Paper = 2,
Scissors = 3,
}
#[derive(Debug, PartialEq, Eq)]
pub struct Strategy {
pub elf: Play,
pub you: Play,
}
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),
}) + input.you as usize //play enum has value corresponding to its score.
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_calc_score() {
let input = Strategy {
elf: Play::Scissors,
you: Play::Scissors,
};
assert_eq!(calc_score(&input), 6);
}
}