From e1e65e718a2a8c45f0e384b5ad9512123601af98 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Sat, 21 Sep 2024 09:13:04 +0200 Subject: [PATCH] Wrote program for Day 58 --- README.md | 2 +- .../day58/Cargo.toml | 6 +++ .../day58/src/lib.rs | 54 +++++++++++++++++++ .../day58/src/main.rs | 28 ++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 Week-09/Day-58_Create-A-Dice-Roller/day58/Cargo.toml create mode 100644 Week-09/Day-58_Create-A-Dice-Roller/day58/src/lib.rs create mode 100644 Week-09/Day-58_Create-A-Dice-Roller/day58/src/main.rs diff --git a/README.md b/README.md index 40b465d..48de579 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #55 | [Filter Repeating Character Strings](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-55_Filter_Repeating-Character-Strings) | :white_check_mark: | | Day #56 | [Convert To Hex](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-56_Convert-To-Hex) | :white_check_mark: | | Day #57 | [Magic Sigil Generator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-57_Magic-Sigil-Generator) | :white_check_mark: | -| Day #58 | [Create A Dice Roller](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-58_Create-A-Dice-Roller) | :white_large_square: | +| Day #58 | [Create A Dice Roller](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-58_Create-A-Dice-Roller) | :white_check_mark: | | Day #59 | [Perfectly Balanced](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-59_Perfectly-Balanced) | :white_large_square: | | Day #60 | [A Game Of Threes](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-60_A-Game-Of-Thrones) | :white_large_square: | | Day #61 | [Write A Web Crawler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-61_Write-A-Web-Crawler) | :white_large_square: | diff --git a/Week-09/Day-58_Create-A-Dice-Roller/day58/Cargo.toml b/Week-09/Day-58_Create-A-Dice-Roller/day58/Cargo.toml new file mode 100644 index 0000000..f53dedf --- /dev/null +++ b/Week-09/Day-58_Create-A-Dice-Roller/day58/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day58" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-09/Day-58_Create-A-Dice-Roller/day58/src/lib.rs b/Week-09/Day-58_Create-A-Dice-Roller/day58/src/lib.rs new file mode 100644 index 0000000..c7a265e --- /dev/null +++ b/Week-09/Day-58_Create-A-Dice-Roller/day58/src/lib.rs @@ -0,0 +1,54 @@ +use std::time::{SystemTime, UNIX_EPOCH}; + +pub fn dice_roller(entries: &[&str]) -> Result>, Box> { + let mut rng = Rng::new(); + let mut result = Vec::new(); + + for entry in entries { + let mut result_inner = Vec::new(); + let (num_dice, sum_sides) = match entry.split_once('d') { + Some((num_dice, sum_sides)) => (num_dice.parse::()?, sum_sides.parse::()?), + None => return Err(format!("Invalid entry {entry}").into()), + }; + for _ in 0..num_dice { + let random_number = rng.random_number(1, sum_sides as u128 + 1); + result_inner.push(random_number as u8); + } + result.push(result_inner); + } + + Ok(result) +} + +pub struct Rng { + seed: u128, +} + +impl Rng { + pub fn new() -> Rng { + let time_since_epoch = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("Error in getting time") + .as_millis(); + Rng { + seed: time_since_epoch, + } + } + + fn linear_congruent_generator(&mut self, a: u128, c: u128, m: u128) -> u128 { + let result = (a * self.seed + c) % m; + self.seed = result; + result + } + + pub fn random_number(&mut self, from: u128, to: u128) -> u128 { + let random_number = self.linear_congruent_generator(1103515245, 12345, 2u128.pow(31)); + (random_number % (to - from)) + from + } +} + +impl Default for Rng { + fn default() -> Self { + Self::new() + } +} diff --git a/Week-09/Day-58_Create-A-Dice-Roller/day58/src/main.rs b/Week-09/Day-58_Create-A-Dice-Roller/day58/src/main.rs new file mode 100644 index 0000000..66201bd --- /dev/null +++ b/Week-09/Day-58_Create-A-Dice-Roller/day58/src/main.rs @@ -0,0 +1,28 @@ +use std::{ + io::{self, Write}, + process::exit, +}; + +use day58::dice_roller; + +fn main() { + println!("Enter the dice entries in the format 'NdM' where N is the number of dice and M is the number of sides on the dice."); + io::stdout().flush().unwrap(); + let mut input = String::new(); + io::stdin().read_line(&mut input).unwrap(); + let entries: Vec<&str> = input.trim().split(' ').collect(); + let result = match dice_roller(&entries) { + Ok(result) => result, + Err(err) => { + eprintln!("{}", err); + exit(1); + } + }; + for el in result { + print!("{}:", el.iter().map(|x| *x as u16).sum::()); + for die in el { + print!(" {}", die); + } + println!(); + } +}