sundries

Assorted little morsels for your enjoyment

git clone https://code.pdelong.com/sundries.git

 1use std::collections::VecDeque;
 2
 3use rand::{
 4    distributions::{Distribution, Standard},
 5    Rng, SeedableRng,
 6};
 7
 8enum Options {
 9    Nun,
10    Hey,
11    Gimmel,
12    Shin,
13}
14
15impl Distribution<Options> for Standard {
16    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Options {
17        let index: u8 = rng.gen_range(0..4);
18        match index {
19            0 => Options::Nun,
20            1 => Options::Hey,
21            2 => Options::Gimmel,
22            3 => Options::Shin,
23            _ => unreachable!(),
24        }
25    }
26}
27
28const P: usize = 4;
29const N: i32 = 10;
30
31fn round(rng: &mut impl Rng) -> i32 {
32    let mut players: VecDeque<i32> = [N; P].into();
33
34    let mut pot = 1;
35    let mut rounds = 0;
36    while players.len() > 1 {
37        if pot == 0 {
38            for _ in 0..players.len() {
39                let mut n = players.pop_front().unwrap();
40                n -= 1;
41                pot += 1;
42                if n > 0 {
43                    players.push_back(n);
44                }
45            }
46        }
47
48        let n = players.pop_front().unwrap();
49        let n = match rng.gen::<Options>() {
50            Options::Nun => n,
51            Options::Hey => {
52                let take = (pot + 1) / 2;
53                pot -= take;
54                n + take
55            }
56            Options::Gimmel => {
57                let new = n + pot;
58                pot = 0;
59                new
60            }
61            Options::Shin => {
62                pot += 1;
63                n - 1
64            }
65        };
66
67        if n != 0 {
68            players.push_back(n);
69        }
70
71        rounds += 1;
72    }
73
74    rounds
75}
76
77fn main() {
78    let mut rng = rand::rngs::SmallRng::from_entropy();
79    let mut v: Vec<i32> = (0..1_000_000).map(|_| round(&mut rng)).collect();
80    v.sort();
81    println!(
82        "{} {} {}",
83        v.first().unwrap(),
84        v[v.len() / 2],
85        v.last().unwrap()
86    );
87}