Add correct day13pt2
authorJack Kinsey <j.jameskinsey@gmail.com>
Sun, 22 Dec 2019 05:07:21 +0000 (00:07 -0500)
committerJack Kinsey <j.jameskinsey@gmail.com>
Sun, 22 Dec 2019 05:07:21 +0000 (00:07 -0500)
This is so shitty it practically hurts to look at but I spent enough
time on this stupid part and I'm not cleaning it up. Also the Lanterna
stuff is kind of cool even if it ended up being wildly unnecessary.

src/adventofcode2019/day13.clj

index 9dfe83dae5e7c2ec946d01109067b435c0393349..03aa4fd8840a4080d681ead102eb085d6030e963 100644 (file)
@@ -3,11 +3,8 @@
               [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))