Mercurial > rlm
view src/rlm/light_bot.clj @ 0:78a630e650d2
initial import
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 18 Oct 2011 00:57:08 -0700 |
parents | |
children |
line wrap: on
line source
1 (ns rlm.light-bot2 (:refer-clojure :only [])3 (:require rlm.ns-rlm mobius.base))4 (rlm.ns-rlm/ns-clone mobius.base)5 ;(use 'clojure.contrib.combinatorics)6 (use 'sunil.curry)9 {:loc '[x y]10 :rot '[c]}12 (defrecord tile [height lit?])18 ;;; Robot Actions19 (def cycle-right20 {:N :E21 :E :S22 :S :W23 :W :N})24 (def cycle-left25 {:N :W26 :W :S27 :S :E28 :E :N})30 (defn turn-left [game]31 (update-in game [:pos :rot] cycle-left))33 (defn turn-right [game]34 (update-in game [:pos :rot] cycle-right))36 (def light* {nil nil37 true false38 false true})40 (defn light [{:keys [map pos] :as game}]41 (update-in game [:map (:loc pos) :lit?] light*))43 (def move44 {:N [0,1]45 :E [1,0]46 :S [0,-1]47 :W [-1,0]})49 (defn-decorated50 advance51 [(curry 2)]52 [action {:keys [pos] :as game}]53 (let [proposed (vec (map + (move (:rot pos)) (:loc pos)))]54 (if (get (:map game) proposed false)55 (assoc-in game [:pos :loc] (action proposed game))56 game)))58 (defn jump-gaurd [proposed {:keys [pos] :as game}]59 (if (= (.height ((:map game) proposed)) (inc (.height ((:map game) (:loc pos)))))60 proposed61 (:loc pos)))62 (def jump (advance jump-gaurd))64 (defn forward-gaurd [proposed {:keys [map pos] :as game}]65 (if (= (.height ((:map game) proposed)) (.height ((:map game) (:loc pos))))66 proposed67 (:loc pos)))68 (def forward (advance forward-gaurd));;heehee :)71 (declare commands)73 (defn expand [game moves]74 (reduce (fn [game move] ((commands move) game)) game moves))76 (defn function-1 [{{f1 :f1} :stragety :as game}]77 (expand game f1))79 (defn function-2 [{{f2 :f2} :stragety :as game}]80 (expand game f2))84 (defn all-lit? [game]85 (not (some false? (map #(.lit? %) (vals (:map game))))))88 (defn solves? [game]89 (all-lit? (expand game (:main (:stragety game)))))93 (def commands94 {:jump jump95 :light light96 :turn-left turn-left97 :turn-right turn-right98 :function-1 function-199 :function-2 function-2100 :forward forward})103 ;;; End game implementation108 ;;; Find linear solutions110 (def naturals (iterate inc 1))112 (defvar linear-commands113 (remove #(or (= % :function-2) (= % :function-1)) (keys commands))114 "all the commands ignoring function calls. Branching factor 5.")116 (decorate selections (curry 2))117 (defn breadth-first118 "iterate through the search space in breadth-first-order"119 [coll]120 (mapcat (selections coll) naturals))122 (defn solutions123 "find solutions via breadth first search and returns as a lazy seq"124 [{level-map :map init-pos :pos :as level}]125 (filter #(solves? (assoc-in level [:stragety :main] %)) (breadth-first linear-commands)))127 ;;(decorate selections [(curry 2) memoize])139 ;;;150 ;;;Level One151 (defn level-one-gen [[x y]]152 (cond (= x 0) (tile. 2 nil)153 (= x 7) (tile. 2 nil)154 (= [x y] [3 4]) (tile. 0 false)155 true (tile. 0 nil)))157 (def level-one-map158 (let [domain (selections (range 8) 2)]159 (zipmap (map vec domain) (map level-one-gen domain))))161 (def level-one-solution162 {:main [:forward :forward :light]163 :f1 []164 :f2 []})166 (def level-one-init-pos {:loc [3 2] :rot :N})167 (def game-1168 {:map level-one-map169 :stragety level-one-solution170 :pos level-one-init-pos})172 (def level-1 {:map level-one-map173 :pos level-one-init-pos})