Skip to content

Instantly share code, notes, and snippets.

@Riduidel
Created October 7, 2018 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Riduidel/021bc9a2bddbc4e8341ec9b1495b1117 to your computer and use it in GitHub Desktop.
Save Riduidel/021bc9a2bddbc4e8341ec9b1495b1117 to your computer and use it in GitHub Desktop.
#[cfg(test)] // <1>
mod tests {
// <2>
use super::*; // <3>
#[test]
fn can_evaluate_r_r() {
// <4>
assert_eq!(evaluate(vec!(Color::RED), vec!(Color::RED)), (1, 0)) // <5>
}
#[test]
fn can_evaluate_r_r_b_r_r_r() {
assert_eq!(
evaluate(
vec!(Color::RED, Color::RED, Color::BLUE),
vec!(Color::RED, Color::RED, Color::RED)
),
(2, 0)
)
}
}
#[derive(Clone, Debug, PartialEq)] // <1>
pub enum Color {
RED,
GREEN,
BLUE,
YELLOW,
}
/// Public interface of the evaluation function
/// The return value is *ALWAYS* a two-elements array
pub fn evaluate(secret: Vec<Color>, user: Vec<Color>) -> (i32, i32) {
do_evaluate(secret.clone(), user.as_slice())
}
pub fn do_evaluate(mut secret: Vec<Color>, user: &[Color]) -> (i32, i32) {
let mut returned = (0, 0); // <1>
if let Some((first_user, next_user)) = user.split_first() { // <2>
if let Some(index) = secret.iter().position(|c| c == first_user) { // <3>
if index == 0 {
returned = (1, 0);
} else {
returned = (0, 1);
}
secret.remove(index); // <4>
}
if !next_user.is_empty() {
let other = do_evaluate(secret, next_user); // <5>
returned = (returned.0 + other.0, returned.1 + other.1); // <6>
}
}
returned
}
fn main() {
println!("Hello, world!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment