Hacker News new | past | comments | ask | show | jobs | submit login

First of all, thanks for sharing; it's a very educational piece of code!

I have some generic coding advice though. With slightly better names you can get rid of a lot of the comments. There is a lot of repetition in the code. If you factor that out, you will have even less need for comments. I highly recommend watching some Kevlin Henney talks on such topics, for example https://youtu.be/ZsHMHukIlJY?t=487 (but he has even better talks recently)

The input of the program should be supplied in a more format more natural to humans and let the computer do the transformation into whatever data structure it's comfortable with. So I would propose a simple, multi-line text as an input:

    xxxxxx
    0x000x
    x*0x0x
    xxxx00
    00000x
    xxxx0x
Then the code would look like this:

    (ns maze.solver
        (:require [clojure.string :as str]))

    (def maze-filename "maze.txt")

    (defn parse [maze-str]
     (->> maze-str
          str/trim 
          str/split-lines
          (mapv (partial into []))))

    (def maze (-> maze-filename slurp parse))
You can also just hardwire a maze during development into the source file, since Clojure supports multiline strings:

    (def maze (parse "
    xxxxxx
    0x000x
    x*0x0x
    xxxx00
    00000x
    xxxx0x
    "))
The result of `parse` function will contain character data types instead of 1 character long strings. I think in this case it's actually more readable and more concise too:

    [[\x \x \x \x \x \x]
     [\0 \x \0 \0 \0 \x]
     [\x \* \0 \x \0 \x]
     [\x \x \x \x \0 \0]
     [\0 \0 \0 \0 \0 \x]
     [\x \x \x \x \0 \x]]
Next `println` actually accepts multiple arguments and concatenates them with a space, after stringifying them, so you can drop the extra `(str ...)` wrapping around them.

Then again, the comment is superfluous. It's obvious that you are printing stuff. If you really want to tell what is it, just wrap it in a well-named function. Eg:

    (defn report [paths]
      (println "The maze has" (count paths) "paths.")
      (println "The shortest path in the maze is:" (count (first paths)) "steps long.")
      (println "The path is" (first paths))
      (println "The longest path in the maze is:" (count (last paths)) "steps long.")
      (println "The path is" (last paths)))
So you can just put `(report sorted-paths)` in your `-main`.



I am shamelessly stealing your code.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: