improve graphics
This commit is contained in:
parent
f623a7481a
commit
5852595890
|
|
@ -60,7 +60,5 @@ impl Counter {
|
||||||
|
|
||||||
scores
|
scores
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
47
src/main.rs
47
src/main.rs
|
|
@ -44,10 +44,10 @@ fn main() {
|
||||||
let ty = BallotType::Candidate;
|
let ty = BallotType::Candidate;
|
||||||
let names = match ty {
|
let names = match ty {
|
||||||
BallotType::Party => counter.header.parties.iter().map(|v| v.name.clone()).collect_vec(),
|
BallotType::Party => counter.header.parties.iter().map(|v| v.name.clone()).collect_vec(),
|
||||||
BallotType::Candidate => counter.header.candidates.iter().map(|v| format!("{}: {}",
|
BallotType::Candidate => counter.header.candidates.iter().map(|v| match v.party_id {
|
||||||
v.name,
|
Some(party_id) => format!("{}: {}", v.name, &counter.header.parties[party_id].name),
|
||||||
v.party_id.map_or("IND", |id| &counter.header.parties[id].name),
|
None => v.name.to_owned(),
|
||||||
)).collect_vec(),
|
}).collect_vec(),
|
||||||
};
|
};
|
||||||
let mut running = vec![true; names.len()];
|
let mut running = vec![true; names.len()];
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
|
|
@ -64,18 +64,16 @@ fn main() {
|
||||||
println!();
|
println!();
|
||||||
eprintln!("Running election rounds:");
|
eprintln!("Running election rounds:");
|
||||||
|
|
||||||
let mut scores = Vec::new();
|
let to_percent = |score: f64| score / f64::from(total) * 100.0;
|
||||||
|
|
||||||
for index in 0..(running.len() - winner_count) {
|
for index in 0..(running.len() - winner_count + 1) {
|
||||||
scores = counter.count_all_with_powers_of_2(ty, &running);
|
let scores = counter.count_all_with_powers_of_2(ty, &running);
|
||||||
let (id_worst, score_worst) = match scores.iter().copied().enumerate().filter(|&(i,_)| running[i]).min_by(|a, b| f64::total_cmp(&a.1, &b.1)) {
|
let scores_ordered = scores.iter().copied().enumerate().filter(|&(i,_)| running[i]).sorted_by(|a, b| f64::total_cmp(&b.1, &a.1)).collect_vec();
|
||||||
Some(v) => v,
|
let (id_worst, score_worst) = match scores_ordered.last() {
|
||||||
None => break
|
Some(&v) => v,
|
||||||
};
|
|
||||||
let (id_best, score_best) = match scores.iter().copied().enumerate().filter(|&(i,_)| running[i]).min_by(|a, b| f64::total_cmp(&b.1, &a.1)) {
|
|
||||||
Some(v) => v,
|
|
||||||
None => break,
|
None => break,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
for (&score, &running) in scores.iter().zip(running.iter()) {
|
for (&score, &running) in scores.iter().zip(running.iter()) {
|
||||||
if first {
|
if first {
|
||||||
|
|
@ -90,19 +88,18 @@ fn main() {
|
||||||
}
|
}
|
||||||
running[id_worst] = false;
|
running[id_worst] = false;
|
||||||
|
|
||||||
|
let mut total_votes = 0.0;
|
||||||
|
|
||||||
println!();
|
println!();
|
||||||
eprintln!(" - {index}:");
|
eprintln!("Round {}:", index+1);
|
||||||
eprintln!(" Best: {} ({score_best})", names[id_best]);
|
for (place, &(id, score)) in scores_ordered.iter().take(winner_count).enumerate() {
|
||||||
eprintln!(" Lost: {} ({score_worst})", names[id_worst]);
|
eprintln!(" {}. {}: {score}, {:.2}%", place + 1, names[id], to_percent(score));
|
||||||
|
total_votes += score;
|
||||||
|
}
|
||||||
|
if scores_ordered.len() > winner_count {
|
||||||
|
eprintln!(" {}. {}: {score_worst}, {:.2}%", scores_ordered.len(), names[id_worst], to_percent(score_worst));
|
||||||
|
}
|
||||||
|
eprintln!(" Total: {total_votes}, {:.2}%", to_percent(total_votes.into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let winners = names.iter().enumerate().filter_map(|(index, name)| match running[index] {
|
|
||||||
true => Some((name.as_str(), scores[index])),
|
|
||||||
false => None,
|
|
||||||
}).collect_vec();
|
|
||||||
let total_votes: f64 = scores.iter().sum();
|
|
||||||
|
|
||||||
eprintln!("Winners: {winners:#?}");
|
|
||||||
eprintln!("Total Votes: {}% ({total_votes} / {total})", total_votes / f64::from(total) * 100.0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue