From: Jack Kinsey Date: Thu, 12 Dec 2019 04:09:05 +0000 (-0500) Subject: Fix day11pt1 and add correct day11pt2 X-Git-Url: http://git.jkinsey.net/?a=commitdiff_plain;h=9205379113b838bcad0f87d9dfa770fc7c3d0b83;p=adventofcode2019.git Fix day11pt1 and add correct day11pt2 Also, make the parse-int function parse... any number of any size? --- diff --git a/resources/day11 b/resources/day11 new file mode 100644 index 0000000..8a5faf7 --- /dev/null +++ b/resources/day11 @@ -0,0 +1 @@ +3,8,1005,8,327,1106,0,11,0,0,0,104,1,104,0,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1001,8,0,28,1006,0,42,2,1104,11,10,1006,0,61,2,1005,19,10,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,102,1,8,65,1006,0,4,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,89,1,1108,10,10,1,1103,11,10,1,109,18,10,1006,0,82,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,102,1,8,126,2,109,7,10,1,104,3,10,1006,0,64,2,1109,20,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,101,0,8,163,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,185,2,1109,12,10,2,103,16,10,1,107,11,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,0,8,10,4,10,1001,8,0,219,1,1005,19,10,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,102,1,8,245,2,1002,8,10,1,2,9,10,1006,0,27,1006,0,37,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,102,1,8,281,1006,0,21,3,8,102,-1,8,10,101,1,10,10,4,10,108,0,8,10,4,10,1001,8,0,306,101,1,9,9,1007,9,1075,10,1005,10,15,99,109,649,104,0,104,1,21102,1,847069852568,1,21101,344,0,0,1105,1,448,21101,0,386979963688,1,21101,355,0,0,1105,1,448,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21102,46346031251,1,1,21101,0,402,0,1105,1,448,21102,1,29195594775,1,21101,0,413,0,1105,1,448,3,10,104,0,104,0,3,10,104,0,104,0,21101,0,868498428772,1,21101,0,436,0,1106,0,448,21102,718170641172,1,1,21102,1,447,0,1105,1,448,99,109,2,21202,-1,1,1,21102,40,1,2,21102,1,479,3,21102,1,469,0,1105,1,512,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,474,475,490,4,0,1001,474,1,474,108,4,474,10,1006,10,506,1101,0,0,474,109,-2,2106,0,0,0,109,4,2102,1,-1,511,1207,-3,0,10,1006,10,529,21101,0,0,-3,22101,0,-3,1,22101,0,-2,2,21101,0,1,3,21101,548,0,0,1106,0,553,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,576,2207,-4,-2,10,1006,10,576,21202,-4,1,-4,1106,0,644,22101,0,-4,1,21201,-3,-1,2,21202,-2,2,3,21102,1,595,0,1105,1,553,21201,1,0,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,614,21102,1,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,636,22102,1,-1,1,21102,1,636,0,106,0,511,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0 diff --git a/src/adventofcode2019/day11.clj b/src/adventofcode2019/day11.clj index 12213b9..0265120 100644 --- a/src/adventofcode2019/day11.clj +++ b/src/adventofcode2019/day11.clj @@ -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 @@ -15,28 +16,56 @@ [: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)))) diff --git a/src/adventofcode2019/intcode.clj b/src/adventofcode2019/intcode.clj index 4914fcd..84a9625 100644 --- a/src/adventofcode2019/intcode.clj +++ b/src/adventofcode2019/intcode.clj @@ -31,11 +31,11 @@ [\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)))) @@ -68,15 +68,15 @@ ([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)))) diff --git a/src/adventofcode2019/lib.clj b/src/adventofcode2019/lib.clj index d992475..890ddf5 100644 --- a/src/adventofcode2019/lib.clj +++ b/src/adventofcode2019/lib.clj @@ -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*) #"\."))]