|
2 | 2 | (:use clojure.pprint |
3 | 3 | clojure.core.logic)) |
4 | 4 |
|
5 | | -; Let's explore a simpler system. Imagine we have two rooms, each with two doors. Each door can lead to any of the rooms (including to itself). |
6 | | -; Route: 0 1 0 1 |
7 | | -; Route result: 0 0 1 1 0 |
8 | | -; Map: |
9 | | -; ↕ ↕ |
10 | | -; "0" ↔ "1" |
11 | | -; |
12 | | - |
13 | | -(def plan [0 1 0 1]) |
14 | | -(def labels [0 0 1 1 0]) |
15 | | -(def door-in-room-count 2) |
16 | | -(def room-count 2) |
| 5 | +(def door-in-room-count 6) |
| 6 | +(def room-count 3) |
17 | 7 |
|
18 | 8 | (defmacro with-room [room & body] |
19 | 9 | (let [label (gensym) |
|
88 | 78 | ) |
89 | 79 | ) |
90 | 80 |
|
| 81 | +; Example exploration plan and result: |
| 82 | +(def plan [ 3 0 1 0 4 5 3 5 2 4 2 1]) |
| 83 | +(def result [0 0 1 2 2 1 1 1 1 0 1 0 2]) |
| 84 | + |
| 85 | +(defn facto [rooms] |
| 86 | + (and* |
| 87 | + [ |
| 88 | + (== (:label (nth rooms 0)) (first result)) |
| 89 | + |
| 90 | + (and* |
| 91 | + (map |
| 92 | + (fn [prev-label door-index next-label] |
| 93 | + (or* |
| 94 | + (map-indexed |
| 95 | + (fn [prev-room-index prev-room] |
| 96 | + (and* |
| 97 | + [ |
| 98 | + (== (:label prev-room) prev-label) |
| 99 | + (or* |
| 100 | + (map-indexed |
| 101 | + (fn [next-room-index next-room] |
| 102 | + (and* |
| 103 | + [ |
| 104 | + (== (:label next-room) next-label) |
| 105 | + (== (:room (nth (:doors prev-room) door-index)) next-room-index) |
| 106 | + (or* |
| 107 | + (map |
| 108 | + (fn [next-room-door] |
| 109 | + (and* |
| 110 | + [ |
| 111 | + (== (:door next-room-door) door-index) |
| 112 | + (== (:room next-room-door) prev-room-index) |
| 113 | + ] |
| 114 | + ) |
| 115 | + ) |
| 116 | + (:doors next-room) |
| 117 | + ) |
| 118 | + ) |
| 119 | + ] |
| 120 | + ) |
| 121 | + ) |
| 122 | + rooms |
| 123 | + ) |
| 124 | + ) |
| 125 | + ] |
| 126 | + ) |
| 127 | + ) |
| 128 | + rooms |
| 129 | + ) |
| 130 | + ) |
| 131 | + ) |
| 132 | + result |
| 133 | + plan |
| 134 | + (rest result) |
| 135 | + ) |
| 136 | + ) |
| 137 | + ] |
| 138 | + ) |
| 139 | +) |
| 140 | + |
91 | 141 | (defn do-solve [] |
92 | | - (run* [rooms-q] |
| 142 | + (run 1 [rooms-q] |
93 | 143 | (with-room room0 |
94 | 144 | (with-room room1 |
95 | | - (let [rooms [room0 room1]] |
96 | | - (conde |
97 | | - [ |
98 | | - (setup-roomo rooms room0) |
99 | | - (setup-roomo rooms room1) |
| 145 | + (with-room room2 |
| 146 | + (let [rooms [room0 room1 room2]] |
| 147 | + (conde |
| 148 | + [ |
| 149 | + (setup-roomo rooms room0) |
| 150 | + (setup-roomo rooms room1) |
| 151 | + (setup-roomo rooms room2) |
100 | 152 |
|
101 | | - ; Now, concrete facts: |
102 | | - (== (:doors room0) [{:door 0 :room 0} {:door 1 :room 1}]) |
103 | | - (== (nth (:doors room1) 0) {:door 0 :room 1}) |
104 | | - ; let the engine to guess where the door 1 from the room 1 connects to |
| 153 | + ; Now, concrete facts: |
| 154 | + (facto rooms) |
105 | 155 |
|
106 | | - (== rooms-q rooms) |
107 | | - ] |
| 156 | + (== rooms-q rooms) |
| 157 | + ] |
| 158 | + ) |
108 | 159 | ) |
109 | 160 | ) |
110 | 161 | ) |
|
0 commit comments