### re: Advent of Code 2019 Solution Megathread - Day 13: Care Package VIEW POST

Clojure solution:

(load-file "intcode.clj")

(ns Day13
(:require intcode))

; Adds or updates a tile in the provided map "tiles" given the 3-size output sequence in the format of (x y tile-type)
(defn assoc-tile [tiles tile-output]
(let [x (nth tile-output 0)
y (nth tile-output 1)
type (nth tile-output 2)]
(assoc tiles [x y] type)))

; Takes all outputs from a process state and updates the given map of tiles with them.
(defn update-tiles [tiles state]
(reduce assoc-tile tiles (partition 3 (:outputs state))))

; Loads the game with no quarter provided and returns a map of all tiles that are created.
(let [result (intcode/run code)]
(update-tiles {} result)))

; Counts the amount of tiles of the given type in a tiles map
(defn count-tiles [tiles tile-type]
(count (filter #{tile-type} (vals tiles))))

; In a map of tiles, finds the first key that is associated with the given tile type.
(defn find-tile-position [tiles tile-type]
(first (filter #(= tile-type (tiles %)) (keys tiles))))

; Plays and beats the game, returns the score at the end.
(defn play-game [code]
(loop [state (intcode/run (assoc code 0 2))
tiles (update-tiles {} state)]
(if (:terminated state)
(tiles [-1 0])
(let [ball-position (find-tile-position tiles 4)