X-Git-Url: http://git.jkinsey.net/?p=adventofcode2019.git;a=blobdiff_plain;f=src%2Fadventofcode2019%2Fday11.clj;fp=src%2Fadventofcode2019%2Fday11.clj;h=12213b904841759918a782210ffcf85493ba2253;hp=0000000000000000000000000000000000000000;hb=b8da8334cb639cbbd04c7a96b1311cb644fdf705;hpb=f1a195aa5c74e264f1aa82aea8556d0501b5abf7 diff --git a/src/adventofcode2019/day11.clj b/src/adventofcode2019/day11.clj new file mode 100644 index 0000000..12213b9 --- /dev/null +++ b/src/adventofcode2019/day11.clj @@ -0,0 +1,42 @@ +(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)))