[adventofcode2019.intcode :as i]
[clojure.string :as str]
[clojure.core.match :refer [match]]
- [clojure.math.combinatorics :as combo]
[lanterna.terminal :as ts]])
-(def term (ts/get-terminal :text))
-
(defn find-bounds [tiles]
(let [x-list (map first (keys tiles))
y-list (map second (keys tiles))
(println score)
(flush))
+(defn find-next-paddle-contact [screen prev-ball ball paddle]
+ (let [db (map - ball prev-ball)
+ [dx dy] db
+ futr (take 50 (iterate (partial map + db) ball))
+ rdtn (fn [screen [bx by]]
+ (if (or (= by paddle) (= dx 0))
+ (reduced bx)
+ (reduced bx)
+ #_(case (screen [bx by])
+ 1 (find-next-paddle-contact screen [bx by] [(- bx dx) by] paddle)
+ 2 (find-next-paddle-contact screen [bx by] [(+ bx dx) (dec by)] paddle)
+ screen)))
+ px (reduce rdtn screen futr)]
+ (if (= px screen) 0 px)))
+
(defn run-game [game [[min-x max-x] [min-y max-y]]]
(let [wait-for-output #(= (count (:output %)) 3)
get-next-ic-output (partial i/intcode-until wait-for-output)
new-game (assoc game :input [0])
field (ts/get-terminal :swing)]
- (ts/start field)
- (ts/put-character field \a)
+ #_(ts/start field)
(loop [program-state (get-next-ic-output new-game)
+ screen {}
score 0
framerate 0
- ctr 0]
- (Thread/sleep framerate)
+ ctr 0
+ prev-ball nil
+ ball nil
+ paddle nil
+ joyst 1]
+ #_(Thread/sleep framerate)
(if (:exit program-state)
- (do (ts/stop field)
+ (do #_(ts/stop field)
score)
(let [[x y t] (:output program-state)
[new-score new-screen] (if (and (= x -1) (= y 0))
- [t {}]
+ (do #_(ts/move-cursor field x y)
+ #_(ts/put-string field (str t))
+ [t {}])
[score {[x y] t}])
- input (case (ts/get-key-blocking field {:interval 1 :timeout framerate})
+ screen (merge screen new-screen)
+ #_(case (ts/get-key-blocking field {:interval framerate
+ :timeout framerate})
\a -1
\d 1
- 0)]
- (draw-tiles new-screen field)
+ 0)
+ paddle (if (= t 3) [x y] paddle)
+ prev-ball (if (= t 4) ball prev-ball)
+ ball (if (= t 4) [x y] ball)
+ joyst (if (and prev-ball ball paddle)
+ (compare (find-next-paddle-contact screen prev-ball ball (second paddle)) (first paddle)))]
+ #_(draw-tiles new-screen field)
(recur (get-next-ic-output (assoc program-state
:output []
- :input [input]))
- new-score (if (> ctr 814) 100 0) (inc ctr)))))))
+ :input [joyst]))
+ screen new-score 0 #_(if (> ctr 814) 0 0) (inc ctr)
+ prev-ball ball paddle joyst))))))
(defn day13 []
(let [input (i/get-program (input-file))