Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Latest commit 6239ea0 @bloat Fix valid-path function.

Maze Logic

Solving mazes using Clojure’s core.logic library.

The mazes are generated using some maze generation code in my project Maze Game. Each maze is two element vector containing the size of the maze and a set of paths. The mazes are laid out on a square grid, so the size is the number of cells along one side of the grid and each path is a vector of two adjacent cell indices. The cells are numbered from 0 at the top left to 99 (for a size 10 maze) at the bottom right. Any path not in the list of paths is assumed to be blocked by a wall.

The mazes have no loops in them, so they are essentially trees.

Attempt 1

The first attempt to solve mazes uses only the most basic core.logic functionality. This is my way of learning basic list manipulation in the core.logic style. This also shows how the default is to combine relations with a logical and just by listing them one after the other inside a fresh block, and how conde is required to use logical or.

Source Code

(require 'maze.logic.attempt1)
(in-ns 'maze.logic.attempt1)
(mz-solve a-maze2)
(mz-solve a-maze3)

Attempt 2

This attempt doesn’t introduce anything new in terms on core.logic techniques. Instead I’m trying to speed things up by using a simpler way of representing solutions. Unfortunately it doesn’t seem any faster and still baulks at anything over a 6x6 maze.

Source Code

(require 'maze.logic.attempt2)
(in-ns 'maze.logic.attempt2)
(mz-solve a-maze2)
(mz-solve a-maze3)

Attempt 3

This third attempt introduces core.logic pattern matching using defne and matche. The algorithm is the same, but the functions have been rewritten and most of the usages of firsto and resto (used to assign logic variables to the members of the incoming lists) have been replaced in favour of using defne’s pattern matching capabaility which can do the same job in a much more concise fashion.

Source Code

(require 'maze.logic.attempt3)
(in-ns 'maze.logic.attempt3)
(mz-solve a-maze2)
(mz-solve a-maze3)


This file is part of Maze Logic.

Maze Logic is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Maze Logic is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Maze Logic. If not, see

Something went wrong with that request. Please try again.