Compare commits
8 Commits
b184b2a319
...
inso/forbi
| Author | SHA1 | Date | |
|---|---|---|---|
| eacbb9e426 | |||
| ec152683e0 | |||
| 9979a0f4fe | |||
| 07c9495736 | |||
| a1e23c29f3 | |||
|
|
04b62ca4dd | ||
|
|
d7564300e8 | ||
| 10c1ba6f04 |
53
src/main.rs
53
src/main.rs
@@ -20,8 +20,8 @@ extern crate log;
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
env_logger::init();
|
||||
// tracing_subscriber::fmt::init();
|
||||
|
||||
info!("let's go!");
|
||||
// build our application with some routes
|
||||
let app = Router::new()
|
||||
.route("/", get(|| async { Html(INDEX_HTML.as_str()) }))
|
||||
@@ -51,19 +51,58 @@ struct State {
|
||||
remaining: Vec<String>,
|
||||
}
|
||||
|
||||
|
||||
fn has_forbidden_adjacent(participants: &Vec<String>) -> bool {
|
||||
let forbidden_pairs = vec![("Alice".into(), "Bob".into())];
|
||||
// Check pairs of adjacent elements, including the circular pair (last, first)
|
||||
((1..participants.len()).any(|i| {
|
||||
match (participants[i - 1].as_str(), participants[i].as_str()) {
|
||||
// Check if the pair is forbidden
|
||||
(a, b) if forbidden_pairs.contains(&(a.to_string(), b.to_string())) ||
|
||||
forbidden_pairs.contains(&(b.to_string(), a.to_string())) => {
|
||||
info!("forbidden pair detected : {:?}/{:?} in {:?}", a, b, participants.join(" => "));
|
||||
return true;
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
) || // Check the pair (last, first) for circular adjacency
|
||||
match (participants.last().unwrap().as_str(), participants[0].as_str()) {
|
||||
(a, b) if forbidden_pairs.contains(&(a.to_string(), b.to_string())) ||
|
||||
forbidden_pairs.contains(&(b.to_string(), a.to_string())) => {
|
||||
info!("forbidden pair detected : {:?}/{:?} in {:?}", a, b, participants.join(" => "));
|
||||
return true;
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fn everyone() -> Vec<String> {
|
||||
vec!["Alice".into(), "Bob".into(), "Carol".into(), "Dave".into()]
|
||||
vec!["Alice".into(), "Bob".into(), "Carol".into(), "Dave".into(), "Edgar".into(), "France".into()]
|
||||
}
|
||||
|
||||
impl Default for State {
|
||||
fn default() -> Self {
|
||||
let mut p = everyone();
|
||||
p.shuffle(&mut thread_rng());
|
||||
info!("distribution : {:?}", p.join(" => "));
|
||||
return Self {
|
||||
participants: p,
|
||||
remaining: everyone()
|
||||
let mut tries = 0;
|
||||
while tries < 100 {
|
||||
p.shuffle(&mut thread_rng());
|
||||
|
||||
if !has_forbidden_adjacent(&p) {
|
||||
info!("distribution : {:?}", p.join(" => "));
|
||||
return Self {
|
||||
participants: p,
|
||||
remaining: everyone()
|
||||
}
|
||||
} else {
|
||||
}
|
||||
|
||||
tries += 1;
|
||||
}
|
||||
|
||||
eprintln!("ERROR (could not generate a correct list)");
|
||||
std::process::exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user