switched to workspaces.
This should let me make a cross-day library.
This commit is contained in:
parent
1469c3a32b
commit
242989bb95
57 changed files with 156 additions and 89 deletions
36
days/day02/src/utilities.rs
Normal file
36
days/day02/src/utilities.rs
Normal file
|
@ -0,0 +1,36 @@
|
|||
#[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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue