Add untested day11pt1
authorJack Kinsey <kinsey_john@bah.com>
Wed, 11 Dec 2019 18:12:16 +0000 (13:12 -0500)
committerJack Kinsey <kinsey_john@bah.com>
Wed, 11 Dec 2019 18:12:16 +0000 (13:12 -0500)
src/adventofcode2019/core.clj
src/adventofcode2019/day11.clj [new file with mode: 0644]
src/adventofcode2019/intcode.clj

index ef36fa8b1fbc1e8a9dc160fbd458585d0e4ea3ef..9b83815d5fb7a94ceaec40b73419d94672bf0c51 100644 (file)
@@ -1,7 +1,7 @@
 (ns adventofcode2019.core
     [:require (adventofcode2019 day01 day02 day03 day04 
                                 day05 day06 day07 day08 
-                                day09 day10)])
+                                day09 day10 day11)])
 
 (defn -main 
   ([]
diff --git a/src/adventofcode2019/day11.clj b/src/adventofcode2019/day11.clj
new file mode 100644 (file)
index 0000000..12213b9
--- /dev/null
@@ -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)))
index dc542dd17e8240e49ca7a83efc950a092342ba38..4914fcdda3d71411b8036fc06a5ed5b3c598f7b3 100644 (file)
   ([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))))