Updated program for Day 21

The maze is more random
This commit is contained in:
Mariano Riefolo 2024-08-15 21:45:17 +02:00
parent 56c06f1d7e
commit 9608d466d4

View File

@ -10,6 +10,7 @@ pub fn draw_maze(height: usize, width: usize) -> String {
rng.random_number(0, width as u128) as usize, rng.random_number(0, width as u128) as usize,
), ),
&mut maze, &mut maze,
&mut rng,
); );
let mut res = "+---".repeat(width) + "+"; let mut res = "+---".repeat(width) + "+";
@ -36,32 +37,38 @@ pub fn draw_maze(height: usize, width: usize) -> String {
res res
} }
fn bfs_make_maze(position: (usize, usize), maze: &mut Vec<Vec<(bool, bool, bool)>>) { fn bfs_make_maze(position: (usize, usize), maze: &mut Vec<Vec<(bool, bool, bool)>>, rng: &mut Rng) {
maze[position.0][position.1].2 = true; maze[position.0][position.1].2 = true;
if position.1 + 1 < maze[0].len() && !maze[position.0][position.1 + 1].2 { let mut nums = [0, 1, 2, 3];
for i in (0..4).rev() {
let rand_index = rng.random_number(0, i + 1);
nums.swap(rand_index as usize, i as usize);
}
for num in nums {
if num == 0 && position.1 + 1 < maze[0].len() && !maze[position.0][position.1 + 1].2 {
let mut new_position = position; let mut new_position = position;
new_position.1 += 1; new_position.1 += 1;
maze[position.0][position.1].1 = true; maze[position.0][position.1].1 = true;
bfs_make_maze(new_position, maze); bfs_make_maze(new_position, maze, rng);
} }
if num == 1 && position.1 > 0 && !maze[position.0][position.1 - 1].2 {
if position.1 > 0 && !maze[position.0][position.1 - 1].2 {
let mut new_position = position; let mut new_position = position;
new_position.1 -= 1; new_position.1 -= 1;
maze[new_position.0][new_position.1].1 = true; maze[new_position.0][new_position.1].1 = true;
bfs_make_maze(new_position, maze); bfs_make_maze(new_position, maze, rng);
} }
if position.0 + 1 < maze.len() && !maze[position.0 + 1][position.1].2 { if num == 2 && position.0 + 1 < maze.len() && !maze[position.0 + 1][position.1].2 {
let mut new_position = position; let mut new_position = position;
new_position.0 += 1; new_position.0 += 1;
maze[position.0][position.1].0 = true; maze[position.0][position.1].0 = true;
bfs_make_maze(new_position, maze); bfs_make_maze(new_position, maze, rng);
} }
if position.0 > 0 && !maze[position.0 - 1][position.1].2 { if num == 3 && position.0 > 0 && !maze[position.0 - 1][position.1].2 {
let mut new_position = position; let mut new_position = position;
new_position.0 -= 1; new_position.0 -= 1;
maze[new_position.0][new_position.1].0 = true; maze[new_position.0][new_position.1].0 = true;
bfs_make_maze(new_position, maze); bfs_make_maze(new_position, maze, rng);
}
} }
} }