/
attempt1.clj
83 lines (73 loc) · 2.37 KB
/
attempt1.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(ns maze.logic.attempt1
(:refer-clojure :exclude [==])
(:use [clojure.core logic]
[maze.logic mazes utils]))
(defn valid-path [p mz]
(fresh [h t]
(conde
;; empty path is valid
((emptyo p))
;; or everything below
((firsto p h)
(resto p t)
;; h (the head of the path) is in the maze, in either order
(path-in-maze h mz)
;; The second cell of h is the first cell of the next entry in the path.
(conde
((emptyo t))
((fresh [a b c]
(firsto t [a b])
(== h [c a]))))
;; The rest of the path is valid.
(valid-path t mz)))))
(defn path-starts-at-0 [p]
(fresh [h a b]
(firsto p h)
(== h [a b])
(conde
((== a 0))
((== b 0)))))
(defn path-ends-at-end [p mz]
(let [last-cell (dec (* (first mz) (first mz)))]
(fresh [t]
(resto p t)
(conde
((fresh [a b h]
(firsto p h)
(emptyo t)
(== h [a b])
(== b last-cell)))
((path-ends-at-end t mz))))))
(defn steps-unequal [s1 s2]
(fresh [a b]
(== s2 [a b])
(!= s1 [a b])
(!= s1 [b a])))
(defn mz-solve [maze]
(run 1 [q]
(path-starts-at-0 q)
(path-ends-at-end q maze)
(valid-path q maze)
(distincto q steps-unequal)))
(comment
(mz-solve a-maze2)
(mz-solve a-maze3)
(mz-solve a-maze4)
(mz-solve a-maze5)
(mz-solve a-maze6)
;; After this point its too slow for my computer.
(mz-solve a-maze7)
(mz-solve a-maze8)
(mz-solve a-maze9)
(mz-solve a-maze10))
;; 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 <http://www.gnu.org/licenses/>.