Add untested day11pt1
[adventofcode2019.git] / src / adventofcode2019 / day11.clj
CommitLineData
b8da8334
JK
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]]])
6
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]
12 [:up 0] :left
13 [:down 0] :right
14 [:left 0] :down
15 [:right 0] :up
16 [:up 1] :right
17 [:down 1] :left
18 [:left 1] :down
19 [:right 1] :up))
20 travel (fn [[x y] heading]
21 (case heading
22 :up [x (inc y)]
23 :down [x (dec y)]
24 :left [(dec x) y]
25 :right [(inc x) y]))]
26 (loop [program-state (i/build-state program {:input [0]})
27 tileset {}
28 position [0 0]
29 heading :up]
30 (if (:exit program-state)
31 tileset
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))))))
37
38(defn day11 []
39 (let [input (mapv parse-int (get-list-from-file (input-file) #","))
40 painted-tiles (painterbot-9000 input)]
41 (part1 (count painted-tiles))
42 #_(part2)))