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

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews