From b8da8334cb639cbbd04c7a96b1311cb644fdf705 Mon Sep 17 00:00:00 2001 From: Jack Kinsey Date: Wed, 11 Dec 2019 13:12:16 -0500 Subject: [PATCH] Add untested day11pt1 --- src/adventofcode2019/core.clj | 2 +- src/adventofcode2019/day11.clj | 42 ++++++++++++++++++++++++++++++++ src/adventofcode2019/intcode.clj | 9 ++++++- 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/adventofcode2019/day11.clj diff --git a/src/adventofcode2019/core.clj b/src/adventofcode2019/core.clj index ef36fa8..9b83815 100644 --- a/src/adventofcode2019/core.clj +++ b/src/adventofcode2019/core.clj @@ -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 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))) diff --git a/src/adventofcode2019/intcode.clj b/src/adventofcode2019/intcode.clj index dc542dd..4914fcd 100644 --- a/src/adventofcode2019/intcode.clj +++ b/src/adventofcode2019/intcode.clj @@ -68,8 +68,15 @@ ([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)))) -- 2.38.5