Wrote program for Day 58
This commit is contained in:
parent
7b89e4760a
commit
e1e65e718a
@ -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: |
|
||||
|
6
Week-09/Day-58_Create-A-Dice-Roller/day58/Cargo.toml
Normal file
6
Week-09/Day-58_Create-A-Dice-Roller/day58/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day58"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
54
Week-09/Day-58_Create-A-Dice-Roller/day58/src/lib.rs
Normal file
54
Week-09/Day-58_Create-A-Dice-Roller/day58/src/lib.rs
Normal file
@ -0,0 +1,54 @@
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
pub fn dice_roller(entries: &[&str]) -> Result<Vec<Vec<u8>>, Box<dyn std::error::Error>> {
|
||||
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::<u8>()?, sum_sides.parse::<u8>()?),
|
||||
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()
|
||||
}
|
||||
}
|
28
Week-09/Day-58_Create-A-Dice-Roller/day58/src/main.rs
Normal file
28
Week-09/Day-58_Create-A-Dice-Roller/day58/src/main.rs
Normal file
@ -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::<u16>());
|
||||
for die in el {
|
||||
print!(" {}", die);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user