diff --git a/src/counter.rs b/src/counter.rs index f815bb0..72b821b 100644 --- a/src/counter.rs +++ b/src/counter.rs @@ -40,6 +40,27 @@ impl Counter { scores } + + pub fn count_all_with_powers_of_2(&self, ty: BallotType, running: &[bool]) -> Vec { + let mut scores = vec![0.0; running.len()]; + + for ballot in self.ballots.iter() { + if ballot.ty != ty { + continue; + } + let mut m = 0.5; + for &id in ballot.votes.iter() { + if !running[id] { + continue; + } + scores[id] += m; + m *= 0.5; + } + } + + scores + } + } diff --git a/src/main.rs b/src/main.rs index 078ddce..878a09d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,15 +65,15 @@ fn main() { println!(); eprintln!("Running election rounds:"); - let mut scores: Vec = Vec::new(); + let mut scores: Vec = Vec::new(); for index in 0..(running.len() - winner_count) { - scores = counter.count_primaries(ty, &running); - let (id_worst, score_worst) = match scores.iter().copied().enumerate().filter(|&(i,_)| running[i]).min_by(|a, b| u32::cmp(&a.1, &b.1)) { + 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)) { Some(v) => v, None => break }; - let (id_best, score_best) = match scores.iter().copied().enumerate().filter(|&(i,_)| running[i]).min_by(|a, b| u32::cmp(&b.1, &a.1)) { + 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, };