From 56c06f1d7e3a762c6b1a360956fd3616d551fa5e Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Thu, 15 Aug 2024 18:12:41 +0200 Subject: [PATCH] Wrote program for Day 21 --- README.md | 2 +- .../day21/Cargo.toml | 6 ++ .../day21/src/lib.rs | 99 +++++++++++++++++++ .../day21/src/main.rs | 23 +++++ 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 Week-03/Day-21_Random-Maze-Generator/day21/Cargo.toml create mode 100644 Week-03/Day-21_Random-Maze-Generator/day21/src/lib.rs create mode 100644 Week-03/Day-21_Random-Maze-Generator/day21/src/main.rs diff --git a/README.md b/README.md index 801a338..7440fd2 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #18 | [Unique Paths](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-18_Unique-Paths) | :white_check_mark: | | Day #19 | [URL Shortener](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-19_URL-Shortener) | :white_check_mark: | | Day #20 | [API Challenge](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-20_API-Challenge) | :white_check_mark: | -| Day #21 | [Random Maze Generator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-21_Random-Maze-Generator) | :white_large_square: | +| Day #21 | [Random Maze Generator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-21_Random-Maze-Generator) | :white_check_mark: | | Day #22 | [Marcio Mellos Challenge](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-22_Marcio-Mellos-Challenge) | :white_large_square: | | Day #23 | [The Dining Philosophers](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-23_The-Dining_Philosophers) | :white_large_square: | | Day #24 | [The Josephus Problem](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-24_The-Josephus-Problem) | :white_large_square: | diff --git a/Week-03/Day-21_Random-Maze-Generator/day21/Cargo.toml b/Week-03/Day-21_Random-Maze-Generator/day21/Cargo.toml new file mode 100644 index 0000000..ed0c318 --- /dev/null +++ b/Week-03/Day-21_Random-Maze-Generator/day21/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day21" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-03/Day-21_Random-Maze-Generator/day21/src/lib.rs b/Week-03/Day-21_Random-Maze-Generator/day21/src/lib.rs new file mode 100644 index 0000000..8c356b1 --- /dev/null +++ b/Week-03/Day-21_Random-Maze-Generator/day21/src/lib.rs @@ -0,0 +1,99 @@ +use std::time::{SystemTime, UNIX_EPOCH}; + +pub fn draw_maze(height: usize, width: usize) -> String { + let mut maze = vec![vec![(false, false, false); width]; height]; + let mut rng = Rng::new(); + + bfs_make_maze( + ( + rng.random_number(0, height as u128) as usize, + rng.random_number(0, width as u128) as usize, + ), + &mut maze, + ); + + let mut res = "+---".repeat(width) + "+"; + for c in 0..height { + res = format!("{}\n|", res); + for r in maze.get(c).unwrap() { + res = format!("{} ", res); + if r.1 { + res = format!("{} ", res); + } else { + res = format!("{}|", res); + } + } + res = format!("{}\n+", res); + for r in maze.get(c).unwrap() { + if r.0 { + res = format!("{} +", res); + } else { + res = format!("{}---+", res); + } + } + } + + res +} + +fn bfs_make_maze(position: (usize, usize), maze: &mut Vec>) { + maze[position.0][position.1].2 = true; + if position.1 + 1 < maze[0].len() && !maze[position.0][position.1 + 1].2 { + let mut new_position = position; + new_position.1 += 1; + maze[position.0][position.1].1 = true; + bfs_make_maze(new_position, maze); + } + + if position.1 > 0 && !maze[position.0][position.1 - 1].2 { + let mut new_position = position; + new_position.1 -= 1; + maze[new_position.0][new_position.1].1 = true; + bfs_make_maze(new_position, maze); + } + if position.0 + 1 < maze.len() && !maze[position.0 + 1][position.1].2 { + let mut new_position = position; + new_position.0 += 1; + maze[position.0][position.1].0 = true; + bfs_make_maze(new_position, maze); + } + if position.0 > 0 && !maze[position.0 - 1][position.1].2 { + let mut new_position = position; + new_position.0 -= 1; + maze[new_position.0][new_position.1].0 = true; + bfs_make_maze(new_position, maze); + } +} + +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-03/Day-21_Random-Maze-Generator/day21/src/main.rs b/Week-03/Day-21_Random-Maze-Generator/day21/src/main.rs new file mode 100644 index 0000000..becec54 --- /dev/null +++ b/Week-03/Day-21_Random-Maze-Generator/day21/src/main.rs @@ -0,0 +1,23 @@ +use day21::draw_maze; +use std::io::{self, Write}; + +fn read_number(output: &str) -> usize { + let mut buffer = String::new(); + + print!("{}", output); + io::stdout().flush().unwrap(); + + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + + buffer.trim().parse().expect("Not a number") +} + +fn main() { + let height = read_number("Enter height: "); + let width = read_number("Enter width: "); + + let maze = draw_maze(height, width); + println!("{}", maze); +}