--- /dev/null
+(ns adventofcode2019.day11
+ [:require [adventofcode2019.lib :refer :all]
+ [adventofcode2019.intcode :as i]
+ [clojure.string :as str]
+ [clojure.core.match :refer [match]]])
+
+(defn painterbot-9000 [program]
+ (let [stop-on-output #(= (count (:output %)) 2)
+ get-next-ic-output (partial i/intcode-until stop-on-output)
+ rotate (fn [heading turning]
+ (match [heading turning]
+ [:up 0] :left
+ [:down 0] :right
+ [:left 0] :down
+ [:right 0] :up
+ [:up 1] :right
+ [:down 1] :left
+ [:left 1] :down
+ [:right 1] :up))
+ travel (fn [[x y] heading]
+ (case heading
+ :up [x (inc y)]
+ :down [x (dec y)]
+ :left [(dec x) y]
+ :right [(inc x) y]))]
+ (loop [program-state (i/build-state program {:input [0]})
+ tileset {}
+ position [0 0]
+ heading :up]
+ (if (:exit program-state)
+ tileset
+ (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))))))
+
+(defn day11 []
+ (let [input (mapv parse-int (get-list-from-file (input-file) #","))
+ painted-tiles (painterbot-9000 input)]
+ (part1 (count painted-tiles))
+ #_(part2)))
([program settings]
(merge (build-state program) settings)))
+(defn intcode-until [pred state]
+ (as-> (assoc state :step true) it
+ (iterate intcode it)
+ (drop-while #(or (not (:exit %)) (pred %)) it)
+ (first it)
+ (dissoc it :step)))
+
(defn intcode [{:as state :keys [memory output]}]
(cond ; quit if :exit, step and return state if :step, else loop
- (get state :exit) {:memory memory :output output}
+ (get state :exit) {:memory memory :output output :exit true}
(get state :step) (perform-operation state)
:else (recur (perform-operation state))))