diff --git a/src/euler84/src/lib.rs b/src/euler84/src/lib.rs index 1605874..7f877a9 100644 --- a/src/euler84/src/lib.rs +++ b/src/euler84/src/lib.rs @@ -1,4 +1,5 @@ use std::collections::{HashMap, VecDeque}; +use std::fmt::Display; use std::iter::Cycle; use rand::prelude::*; @@ -92,6 +93,17 @@ enum Chance { DoNothing6, } +impl Display for Squares { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let value = *self as isize; + if value < 10 { + f.write_fmt(format_args!("0{value}")) + } else { + f.write_fmt(format_args!("{value}")) + } + } +} + #[allow(dead_code)] #[derive(Debug)] pub enum Dice { @@ -176,8 +188,7 @@ impl Monopoly { } fn go_to_jail(&mut self) -> Squares { - let _ = (&mut self.board).skip_while(|c| *c != Squares::Jail); - Squares::Jail + self.go_to_square(Squares::Jail) } fn handle_chance(&mut self, square: Squares) -> Squares { @@ -216,8 +227,7 @@ impl Monopoly { } fn advance_to_go(&mut self) -> Squares { - let _ = (&mut self.board).skip_while(|c| *c != Squares::Go); - Squares::Go + self.go_to_square(Squares::Go) } fn draw_from_chance_pile_and_put_at_bottom(&mut self) -> &Chance { @@ -288,9 +298,11 @@ impl Statistics { .map(|(k, v)| (k, *v as f64 / self.iterations as f64 * 100.)) .collect(); result.sort_unstable_by(|(_, v1), (_, v2)| v2.partial_cmp(v1).unwrap()); - for (s, v) in result { + for (s, v) in &result { println!("{s:?}: {v:.2}"); } + let output: String = result.iter().take(3).map(|(s, _)| s.to_string()).collect(); + println!("\n{output}"); } } @@ -344,6 +356,7 @@ mod test { fn custom() { let mut monopoly = Monopoly::new(Dice::SixSided); assert_eq!(monopoly.go_to_square(Squares::E3), Squares::E3); + assert_eq!(monopoly.advance_by(1), Squares::R3); } #[test] diff --git a/src/euler84/src/main.rs b/src/euler84/src/main.rs index cddd597..ed0dd20 100644 --- a/src/euler84/src/main.rs +++ b/src/euler84/src/main.rs @@ -1,8 +1,8 @@ use euler84::{Dice, Monopoly, Statistics}; fn main() { - let monopoly = Monopoly::new(Dice::SixSided); - let mut statistics = Statistics::new(monopoly, 50_000_000); + let monopoly = Monopoly::new(Dice::FourSided); + let mut statistics = Statistics::new(monopoly, 500_000_000); statistics.run(); statistics.show(); }