1 (ns adventofcode2019.day11
2 [:require [adventofcode2019.lib :refer :all]
3 [adventofcode2019.intcode :as i]
4 [clojure.string :as str]
5 [clojure.core.match :refer [match]]])
7 (defn painterbot-9000 [program]
8 (let [stop-on-output #(= (count (:output %)) 2)
9 get-next-ic-output (partial i/intcode-until stop-on-output)
10 rotate (fn [heading turning]
11 (match [heading turning]
20 travel (fn [[x y] heading]
26 (loop [program-state (i/build-state program {:input [0]})
30 (if (:exit program-state)
32 (let [next-state (get-next-ic-output program-state)
33 [color direction] (:output next-state)
34 new-heading (rotate heading direction)]
35 (recur (assoc next-state :output []) (assoc tileset position color)
36 (travel position new-heading) new-heading))))))
39 (let [input (mapv parse-int (get-list-from-file (input-file) #","))
40 painted-tiles (painterbot-9000 input)]
41 (part1 (count painted-tiles))