]> localhost Git - adventofcode2019.git/commitdiff
Fix day11pt1 and add correct day11pt2
authorJack Kinsey <j.jameskinsey@gmail.com>
Thu, 12 Dec 2019 04:09:05 +0000 (23:09 -0500)
committerJack Kinsey <j.jameskinsey@gmail.com>
Thu, 12 Dec 2019 04:09:05 +0000 (23:09 -0500)
Also, make the parse-int function parse... any number of any size?

src/adventofcode2019/day11.clj
src/adventofcode2019/intcode.clj
src/adventofcode2019/lib.clj

index 12213b904841759918a782210ffcf85493ba2253..026512088204895cf56aae021c55f87037249fa5 100644 (file)
@@ -4,9 +4,10 @@
               [clojure.string :as str]
               [clojure.core.match :refer [match]]])
 
-(defn painterbot-9000 [program]
+(defn painterbot-9000 [program input]
   (let [stop-on-output #(= (count (:output %)) 2)
         get-next-ic-output (partial i/intcode-until stop-on-output)
+        start-state (i/build-state program {:input [input]})
         rotate (fn [heading turning]
                  (match [heading turning]
                         [:up    0] :left
                         [:right 0] :up
                         [:up    1] :right
                         [:down  1] :left
-                        [:left  1] :down
-                        [:right 1] :up))
+                        [:left  1] :up
+                        [:right 1] :down))
         travel (fn [[x y] heading]
                  (case heading
-                   :up    [x (inc y)]
-                   :down  [x (dec y)]
+                   :up    [x (dec y)]
+                   :down  [x (inc y)]
                    :left  [(dec x) y]
                    :right [(inc x) y]))]
-    (loop [program-state (i/build-state program {:input [0]})
+    (loop [program-state (get-next-ic-output start-state)
            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))))))
+        (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))))
 
 (defn day11 []
   (let [input (mapv parse-int (get-list-from-file (input-file) #","))
-        painted-tiles (painterbot-9000 input)] 
+        painted-tiles (painterbot-9000 input 0)
+        reg-id-tiles (painterbot-9000 input 1)] 
     (part1 (count painted-tiles))
-    #_(part2)))
+    (part2 "see below")
+    (run! println (draw-tiles reg-id-tiles))))
index 4914fcdda3d71411b8036fc06a5ed5b3c598f7b3..84a96254126ed52d7dcbb53c4e8c808be06aced9 100644 (file)
         [\0 \3] (fn [S a _ _] ; IN
                   (-> S
                       (assoc-in [:memory (a S true)] (first (:input S)))
-                      (update :input subvec 1)
+                      (update :input rest)
                       (update :ctr + 2)))
         [\0 \4] (fn [S a _ _] ; OUT
                   (-> S
-                      (update :output conj (or (get-in S [:memory (a S true)]) 0))
+                      (update :output conj (a S))
                       (update :ctr + 2))) 
         [\0 \5] (fn [S a b _] ; BNEQ
                    (update S :ctr (if (not= (a S) 0) (constantly (b S)) #(+ % 3))))
   ([program settings]
    (merge (build-state program) settings)))
 
+(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 :exit true}
+    (get state :step) (perform-operation state)
+    :else (recur (perform-operation state))))
+
 (defn intcode-until [pred state]
   (as-> (assoc state :step true) it
         (iterate intcode it)
-        (drop-while #(or (not (:exit %)) (pred %)) it)
+        (drop-while #(not (or (: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 :exit true}
-       (get state :step) (perform-operation state)
-       :else (recur (perform-operation state))))
index d992475caf866b9d7f992668c6bba59ec390ab53..890ddf5b8cef7ca064c5608f61736a675aaee8e0 100644 (file)
@@ -1,5 +1,6 @@
 (ns adventofcode2019.lib
     [:require [clojure.string :as str]
+              [clojure.edn :as edn]
               [clojure.java.io :as io]
               [clojure.java.shell :refer [sh]]])
 
@@ -9,8 +10,11 @@
   ([file-name split-regex]
     (str/split (str/trim (slurp file-name)) split-regex)))
 
-(def parse-int
-  #(Integer/parseInt %))
+(defn parse-int [n]
+  (let [n-val (edn/read-string n)]
+    (if (number? n-val)
+        n-val
+        (throw (Exception. "Not a number!")))))
 
 (defmacro input-file []
   (let [bottom-ns (last (str/split (str *ns*) #"\."))]