kbsymanz

kbsymanz

I’ve been learning Elixir for about 10 months now in my spare time and I would welcome some constructive feedback on my Maze Generator library.

https://github.com/kbsymanz/maze_generator

Thanks!

Kurt Symanzik

Showing Posts 1 to 5

kokolegorille

kokolegorille

Hello and welcome,

Disclaimer… I did not read the book Mazes for programmers.

I do not like the use of Agent to store visited cells, just to manage a map. It is started/stopped in the same function.

It’s possible to do without processes. I translated your code to Rust, for fun (and learning).

pub struct RecursiveBacktrack {
    visited: HashMap<Coordinate, bool>
}

impl RecursiveBacktrack {
    pub fn new() -> Self {
        Self {
            visited: HashMap::new(),
        }
    }

    pub fn carve(&mut self, grid: &mut Grid) {
        let mut rng = thread_rng();

        let x: usize = rng.gen_range(0, grid.width);
        let y: usize = rng.gen_range(0, grid.height);

        let starting_coordinate = Coordinate(x, y);

        self.do_carve(grid, starting_coordinate, starting_coordinate)
    }

    fn do_carve(&mut self, grid: &mut Grid, current: Coordinate, last: Coordinate) {
        match self.visited.get(&current) {
            Some(_) => (),
            None => {
                self.visited.insert(current, true);
                grid.open_passage(current, last);

                let mut neighbors = grid.neighbors(&current);
                neighbors.shuffle(&mut thread_rng());

                for neighbor in neighbors {
                    self.do_carve(grid, neighbor, current);
                }
            },
        }
    }
}

and the result

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
+---+---+   +   +   +---+   +   +   +   +   +---+   +   +   +   +---+   +   +   +
|               |           |   |       |   |           |   |   |       |   |    
+---+   +---+---+   +---+   +---+   +---+   +   +---+---+   +   +   +---+   +   +
|       |           |   |       |   |       |   |           |   |   |       |    
+---+---+   +---+   +   +   +---+   +---+   +---+   +---+   +---+   +   +---+   +
|           |   |   |       |           |           |   |           |   |   |   |
+---+---+---+   +   +   +---+   +---+   +   +---+   +   +   +---+   +   +   +   +
|           |   |   |   |       |   |   |   |   |   |   |   |   |   |       |    
+---+---+---+   +   +   +---+   +---+   +   +   +---+   +   +---+   +---+---+---+
|   |           |   |       |           |   |           |                        
+---+   +---+---+   +---+   +   +---+---+   +---+---+   +---+   +---+---+   +---+
|               |       |   |   |               |   |       |   |           |    
+---+---+---+   +---+---+   +   +   +---+---+   +   +   +---+   +---+---+---+   +
|       |   |               |       |       |   |   |   |       |                
+---+   +   +---+---+   +---+---+---+   +---+   +   +---+   +   +   +---+---+---+
|                       |               |           |       |   |   |   |        
+---+---+---+---+---+   +   +---+---+---+   +---+---+   +---+   +   +   +   +---+
|           |       |   |               |   |           |   |       |   |   |    
+---+---+   +   +---+---+   +---+---+   +   +---+   +---+   +---+---+---+   +   +
|       |   |   |           |       |   |       |                           |   |
+---+   +---+   +   +   +   +---+   +   +---+   +---+   +---+---+---+   +   +   +
|   |       |   |   |   |   |   |           |       |   |               |   |   |
+---+---+   +   +   +---+   +---+   +---+   +---+---+   +   +   +---+---+   +   +
|       |   |   |                   |   |           |   |   |   |   |       |    
+---+   +   +   +---+   +---+   +---+---+   +---+   +   +   +   +   +   +   +---+
|   |   |           |   |   |   |           |   |   |   |   |   |   |   |   |    
+---+   +   +---+   +---+---+   +   +---+---+   +   +   +   +   +   +---+---+   +
|       |   |   |               |                   |   |   |   |               |
+---+---+   +   +---+---+---+   +---+   +   +---+---+   +---+   +   +   +---+   +
|       |   |               |       |   |   |   |               |   |   |        
+---+---+   +---+   +   +---+   +   +   +---+   +   +---+---+   +---+   +---+---+
|               |   |           |   |   |       |   |       |               |    
+---+   +---+   +---+   +---+---+---+   +---+---+   +   +---+   +---+---+   +   +
|       |   |       |   |                           |   |   |           |   |    
+---+---+   +---+   +---+   +   +---+---+---+---+---+   +   +---+---+---+   +---+
|   |           |           |   |                       |                        
+---+   +---+   +   +---+   +---+   +---+   +---+   +---+---+---+---+---+---+---+
|       |   |   |   |   |       |   |   |   |   |   |           |           |    
+---+   +   +   +   +   +   +   +   +   +   +   +   +   +---+   +   +---+   +   +

I don’t know if Agent are used to solve the maze, but You can generate without :slight_smile:

kbsymanz

kbsymanz OP

Thank you for the suggestion - I will do that. I’m curious to find out how that affects performance.

Thanks

kokolegorille

kokolegorille

I did not benchmark, but I am sure calling a pure function will be faster than starting and passing messages to an Agent :slight_smile:

kbsymanz

kbsymanz OP

I refactored to use a map instead of an agent and it confirmed your suggestion. The result was over 3 times faster. Thanks!

kokolegorille

kokolegorille

Whenever You want speed, real speed, You can use an ETS table.

It is blazing fast.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews