Wrote program for Day 21

This commit is contained in:
Mariano Riefolo 2024-08-15 18:12:41 +02:00
parent 8aa13b7c38
commit 56c06f1d7e
4 changed files with 129 additions and 1 deletions

View File

@ -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 #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 #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 #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 #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 #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: | | 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: |

View File

@ -0,0 +1,6 @@
[package]
name = "day21"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -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<Vec<(bool, bool, bool)>>) {
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()
}
}

View File

@ -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);
}