Last active
June 4, 2018 01:18
-
-
Save kunigami/9ba7264ea3f00998595b0c92d9d9ccb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn group_possibilities_by_score( | |
guess: &[i32; N], | |
possibilities: &Vec<[i32; N]> | |
) -> [Vec<[i32; N]>; NN] { | |
let mut possibilities_by_score: [Vec<[i32; N]>; NN] = Default::default(); | |
for i in 0..NN { | |
possibilities_by_score[i] = vec![]; | |
} | |
for possibility in possibilities { | |
let mut new_possibility = possibility.clone(); | |
let score = compute_score(&guess, &new_possibility); | |
let score_index = encode_score(score); | |
possibilities_by_score[score_index].push(new_possibility) | |
} | |
return possibilities_by_score; | |
} | |
fn compute_score(guess: &[i32; N], secret: &[i32; N]) -> (i32, i32) { | |
let mut perfect_matches = 0; | |
for i in 0..guess.len() { | |
if guess[i] == secret[i] { | |
perfect_matches += 1; | |
} | |
} | |
let mut look_up_position: [bool; D] = [false; D]; | |
for i in 0..guess.len() { | |
let position = (guess[i] - 1) as usize; | |
look_up_position[position] = true; | |
} | |
let mut any_matches = 0; | |
for i in 0..secret.len() { | |
let position = (secret[i] - 1) as usize; | |
if look_up_position[position] { | |
any_matches += 1; | |
} | |
} | |
let imperfect_matches = any_matches - perfect_matches; | |
return (perfect_matches, imperfect_matches); | |
} | |
fn encode_score(score: (i32, i32)) -> usize { | |
let n = (N + 1) as i32; | |
return (score.0 * n + score.1) as usize; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment