- (let [next-state (get-next-ic-output program-state)
- [color direction] (:output next-state)
- new-heading (rotate heading direction)]
- (recur (assoc next-state :output []) (assoc tileset position color)
- (travel position new-heading) new-heading))))))
+ (let [[color direction] (:output program-state)
+ new-heading (rotate heading direction)
+ new-position (travel position new-heading)
+ prep-state (assoc program-state
+ :output []
+ :input [(get tileset new-position 0)])]
+ (recur (get-next-ic-output prep-state)
+ (assoc tileset position color)
+ new-position new-heading))))))
+
+(defn find-bounds [tiles]
+ (let [x-list (map first (keys tiles))
+ y-list (map second (keys tiles))
+ min-x (reduce min x-list)
+ max-x (reduce max x-list)
+ min-y (reduce min y-list)
+ max-y (reduce max y-list)]
+ [[min-x max-x] [min-y max-y]]))
+
+(defn make-field [[dx dy]]
+ (let [line (vec (repeat dx \space))]
+ (mapv (constantly line) (range dy))))
+
+(defn draw-tiles [tiles]
+ (let [[[min-x max-x] [min-y max-y]] (find-bounds tiles)
+ to-text #(match % 0 \space
+ 1 \u2588) ; full-block
+ field (make-field [(- max-x min-x -1) (- max-y min-y -1)])
+ paint-tile (fn [field [x y] color]
+ (assoc-in field [(- y min-y) (- x min-x)] (to-text color)))]
+ (mapv str/join (reduce-kv paint-tile field tiles))))