Add broken day13pt2
authorJack Kinsey <j.jameskinsey@gmail.com>
Fri, 20 Dec 2019 04:09:22 +0000 (23:09 -0500)
committerJack Kinsey <j.jameskinsey@gmail.com>
Fri, 20 Dec 2019 04:09:22 +0000 (23:09 -0500)
Also add miscellaneous input files.

project.clj
resources/day13 [new file with mode: 0644]
resources/day14 [new file with mode: 0644]
resources/day16 [new file with mode: 0644]
src/adventofcode2019/day13.clj
src/adventofcode2019/intcode.clj

index d498865c56d83c4e77707ba4b0bff5162f796006..7e487cb4dfe94d8076e238ec11ac1536a2708d96 100644 (file)
@@ -7,7 +7,8 @@
                  [org.clojure/core.match "0.3.0"]
                  [org.clojure/math.combinatorics "0.1.6"]
                  [org.clojure/core.async "0.6.532"]
-                 [org.clojure/data.priority-map "0.0.10"]]
+                 [org.clojure/data.priority-map "0.0.10"]
+                 [clojure-lanterna "0.9.4"]]
   :main ^:skip-aot adventofcode2019.core
   :target-path "target/%s"
   :profiles {:uberjar {:aot :all}})
diff --git a/resources/day13 b/resources/day13
new file mode 100644 (file)
index 0000000..8555ee8
--- /dev/null
@@ -0,0 +1 @@
+1,380,379,385,1008,2267,610415,381,1005,381,12,99,109,2268,1101,0,0,383,1101,0,0,382,20102,1,382,1,20101,0,383,2,21101,37,0,0,1106,0,578,4,382,4,383,204,1,1001,382,1,382,1007,382,37,381,1005,381,22,1001,383,1,383,1007,383,22,381,1005,381,18,1006,385,69,99,104,-1,104,0,4,386,3,384,1007,384,0,381,1005,381,94,107,0,384,381,1005,381,108,1105,1,161,107,1,392,381,1006,381,161,1101,-1,0,384,1106,0,119,1007,392,35,381,1006,381,161,1101,0,1,384,21001,392,0,1,21102,1,20,2,21101,0,0,3,21102,138,1,0,1105,1,549,1,392,384,392,21002,392,1,1,21101,0,20,2,21101,3,0,3,21101,161,0,0,1106,0,549,1101,0,0,384,20001,388,390,1,20101,0,389,2,21102,1,180,0,1105,1,578,1206,1,213,1208,1,2,381,1006,381,205,20001,388,390,1,21002,389,1,2,21101,205,0,0,1106,0,393,1002,390,-1,390,1102,1,1,384,21002,388,1,1,20001,389,391,2,21102,228,1,0,1106,0,578,1206,1,261,1208,1,2,381,1006,381,253,20101,0,388,1,20001,389,391,2,21101,253,0,0,1105,1,393,1002,391,-1,391,1101,1,0,384,1005,384,161,20001,388,390,1,20001,389,391,2,21102,1,279,0,1105,1,578,1206,1,316,1208,1,2,381,1006,381,304,20001,388,390,1,20001,389,391,2,21101,304,0,0,1105,1,393,1002,390,-1,390,1002,391,-1,391,1101,1,0,384,1005,384,161,20102,1,388,1,20101,0,389,2,21101,0,0,3,21102,338,1,0,1105,1,549,1,388,390,388,1,389,391,389,20102,1,388,1,20101,0,389,2,21101,0,4,3,21102,1,365,0,1106,0,549,1007,389,21,381,1005,381,75,104,-1,104,0,104,0,99,0,1,0,0,0,0,0,0,265,16,17,1,1,18,109,3,21202,-2,1,1,21201,-1,0,2,21102,1,0,3,21102,414,1,0,1105,1,549,22101,0,-2,1,21202,-1,1,2,21101,429,0,0,1105,1,601,2102,1,1,435,1,386,0,386,104,-1,104,0,4,386,1001,387,-1,387,1005,387,451,99,109,-3,2106,0,0,109,8,22202,-7,-6,-3,22201,-3,-5,-3,21202,-4,64,-2,2207,-3,-2,381,1005,381,492,21202,-2,-1,-1,22201,-3,-1,-3,2207,-3,-2,381,1006,381,481,21202,-4,8,-2,2207,-3,-2,381,1005,381,518,21202,-2,-1,-1,22201,-3,-1,-3,2207,-3,-2,381,1006,381,507,2207,-3,-4,381,1005,381,540,21202,-4,-1,-1,22201,-3,-1,-3,2207,-3,-4,381,1006,381,529,22101,0,-3,-7,109,-8,2106,0,0,109,4,1202,-2,37,566,201,-3,566,566,101,639,566,566,2102,1,-1,0,204,-3,204,-2,204,-1,109,-4,2105,1,0,109,3,1202,-1,37,593,201,-2,593,593,101,639,593,593,21001,0,0,-2,109,-3,2105,1,0,109,3,22102,22,-2,1,22201,1,-1,1,21102,1,409,2,21102,1,463,3,21102,1,814,4,21102,1,630,0,1106,0,456,21201,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2,0,0,0,0,2,0,2,2,0,2,0,2,2,0,1,1,0,0,2,2,2,2,0,2,2,0,0,0,0,0,0,0,0,0,2,2,2,2,0,0,0,0,2,0,2,2,2,0,2,0,0,1,1,0,2,0,2,2,2,2,2,2,0,0,0,0,0,0,2,2,0,2,2,2,2,0,0,2,2,2,2,2,0,0,0,2,2,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,72,10,67,45,58,25,55,73,97,49,19,51,58,95,30,82,74,9,98,96,38,64,30,45,14,73,42,5,3,61,68,23,18,14,9,16,21,7,77,39,38,16,82,17,58,87,90,64,52,1,96,67,66,16,65,15,22,41,69,90,93,92,96,45,68,17,63,51,15,61,51,93,65,55,42,76,48,52,31,98,6,88,69,65,65,30,51,88,4,13,36,90,80,23,31,42,63,86,52,15,79,78,59,77,57,71,84,81,73,56,1,5,7,86,75,31,63,76,21,73,16,41,86,15,78,85,2,79,63,54,79,65,87,13,86,96,81,69,27,76,8,48,5,79,10,74,76,86,95,55,72,52,23,41,50,46,68,29,86,61,96,29,34,40,86,86,1,20,90,35,69,64,50,51,75,65,93,19,5,15,96,3,88,8,43,66,88,72,84,69,42,4,95,51,80,81,27,75,92,22,45,54,63,51,82,91,13,25,54,41,84,84,29,98,50,91,11,40,69,13,47,42,72,46,87,31,27,98,65,94,26,51,79,39,29,38,42,46,25,36,26,66,12,93,58,1,61,41,37,57,60,60,9,70,63,26,56,1,27,5,11,93,17,48,95,19,79,16,14,16,29,79,56,16,26,37,50,10,38,53,4,10,3,57,20,59,16,51,88,66,74,91,56,42,84,30,36,31,36,58,68,66,91,36,71,30,39,96,50,84,76,95,14,89,75,59,77,66,36,88,62,60,3,45,13,39,48,33,59,21,19,35,90,81,66,52,75,34,70,55,56,47,22,20,87,73,73,76,73,8,96,55,46,5,1,64,27,8,37,87,50,8,79,74,63,26,43,44,2,85,91,28,13,16,15,55,87,94,28,86,66,29,34,46,18,41,37,94,63,31,78,48,17,4,25,62,15,10,18,19,97,50,78,5,79,5,70,64,86,61,58,59,61,5,71,68,14,24,17,56,85,52,64,92,45,90,94,55,47,5,56,59,20,15,41,36,58,55,25,47,45,69,58,36,44,80,94,52,84,17,27,20,44,51,93,10,56,77,45,29,93,63,96,95,47,31,63,69,64,74,53,34,36,20,14,40,30,61,86,15,3,94,61,43,75,59,64,41,34,98,32,65,73,18,30,46,66,38,68,25,96,16,37,54,38,44,26,52,1,2,21,93,37,26,4,45,69,82,59,34,55,34,77,88,46,70,32,56,82,10,20,31,40,20,55,3,3,93,95,65,56,61,68,41,35,62,20,58,55,42,41,40,33,51,6,52,84,27,62,81,32,35,87,97,79,7,97,77,40,48,74,4,6,36,58,59,25,6,5,84,7,44,51,88,37,9,30,29,26,91,41,72,39,24,68,58,49,80,49,43,98,43,92,9,49,64,10,96,50,86,56,2,72,58,80,57,77,61,74,14,42,50,55,40,21,77,20,19,16,80,84,92,27,32,37,80,59,69,13,11,19,6,94,54,88,51,69,41,54,68,36,82,68,19,77,85,37,5,58,61,72,5,67,17,35,29,18,71,46,5,29,8,93,97,36,37,25,93,27,33,93,79,10,84,75,6,91,98,34,32,37,70,18,84,52,32,11,88,44,69,58,92,52,68,77,39,90,9,58,74,1,53,56,64,75,46,59,39,52,32,41,62,81,75,7,93,29,89,51,34,31,93,70,94,30,98,68,3,60,2,2,49,31,15,65,11,78,70,2,50,29,9,9,85,65,52,28,95,55,77,98,29,65,56,51,32,44,42,82,14,29,22,5,29,65,86,84,88,58,63,10,13,13,51,97,17,57,19,39,83,72,93,15,54,31,83,3,43,21,83,74,2,86,47,25,89,20,11,68,80,29,21,58,69,610415
diff --git a/resources/day14 b/resources/day14
new file mode 100644 (file)
index 0000000..0f4a32c
--- /dev/null
@@ -0,0 +1,61 @@
+8 LHFV => 3 PMVMQ
+2 ZXNM, 1 PSVLS, 4 GRDNT, 26 GLZH, 3 VHJX, 16 BGPF, 1 LHVTN => 4 BTQL
+10 NKHSG, 20 FCPC, 11 GRDNT => 5 HDJB
+6 WPZN, 1 LHFV => 7 BGPF
+1 WDXT, 1 PLCNZ => 3 QHFKR
+12 LCHZ => 1 TPXCK
+11 LSNG => 4 XFGH
+195 ORE => 4 GRNC
+8 XFGQ => 1 GRDNT
+1 FBRG => 5 LCHZ
+7 XZBJ, 8 RSZF, 9 SVDX => 9 LWDP
+20 WDXT => 5 RQFRT
+1 LXQWG, 1 GLZH => 6 SDLJ
+4 XFGH => 1 GCZLZ
+1 WPZN => 1 FBRG
+19 XZBJ => 5 WXGV
+1 GDXC => 6 WDXT
+1 WXGV, 1 NKHSG, 2 LWDP => 5 FCNPB
+4 LWDP, 5 BGPF => 9 KLRB
+1 GMRN => 4 GLZH
+1 RQFRT => 5 SVDX
+2 HWKG => 7 LHFV
+2 LCHZ, 13 JTJT, 10 TPXCK => 3 RSZF
+29 MZTVH => 6 TSGR
+9 NRFLK, 1 SVDX => 5 NKHSG
+123 ORE => 9 GDXC
+1 PZPBV, 21 PMVMQ, 1 GCZLZ => 8 SKZGB
+3 GRNC, 5 GDXC => 8 QZVM
+6 VTDQ, 13 TCQW, 3 FCNPB, 48 PSVLS, 3 RLNF, 73 BTQL, 5 MHRVG, 26 BGPF, 26 HDJB, 5 XFGQ, 6 HTFL => 1 FUEL
+5 QZVM, 2 JTJT => 1 PXKHG
+3 LSNG, 1 PMVMQ => 8 VTDQ
+31 XFGH => 1 FCPC
+9 PSVLS => 8 FWGTF
+1 GRNC => 3 WPZN
+16 JBXDX, 4 GRNC => 6 HWKG
+1 SKZGB, 5 RSZF => 4 XZBJ
+134 ORE => 9 CTDRZ
+1 SVDX, 2 TPXCK => 7 JTJT
+6 RQFRT, 4 KBCW => 3 BGNLR
+12 KLRB, 12 LHFV => 4 HTFL
+2 GMRN => 6 XFGQ
+16 WNSW, 12 SKZGB => 8 LXQWG
+2 NRFLK, 2 CTDRZ => 9 JBXDX
+1 PZPBV => 8 RLNF
+2 JTJT, 5 GCZLZ => 3 WNSW
+5 WXGV, 2 LCHZ => 2 SCDS
+1 QHFKR => 3 GMRN
+10 JTJT, 2 HRCG => 8 KBCW
+7 HWKG => 4 PSVLS
+7 WNSW, 1 PXKHG, 3 BGNLR => 9 MZTVH
+15 TPXCK, 11 LHFV => 5 HRCG
+1 LSNG, 1 HWKG => 3 PZPBV
+7 BGPF => 9 PLCNZ
+1 ZGWT => 6 ZXNM
+26 NKHSG, 1 LHFV, 2 JTJT, 26 WXGV, 6 SDLJ, 1 KLRB, 1 TSGR => 8 TCQW
+154 ORE => 4 NRFLK
+1 GMRN => 3 VHJX
+5 QZVM, 3 GDXC => 7 LSNG
+5 WNSW => 5 ZGWT
+6 QHFKR, 8 PZPBV, 10 FBRG, 13 FWGTF, 1 LHVTN, 4 SCDS, 8 VHJX, 7 TSGR => 6 MHRVG
+12 GLZH => 5 LHVTN
diff --git a/resources/day16 b/resources/day16
new file mode 100644 (file)
index 0000000..a424590
--- /dev/null
@@ -0,0 +1 @@
+59766832516471105169175836985633322599038555617788874561522148661927081324685821180654682056538815716097295567894852186929107230155154324411726945819817338647442140954601202408433492208282774032110720183977662097053534778395687521636381457489415906710702497357756337246719713103659349031567298436163261681422438462663511427616685223080744010014937551976673341714897682634253850270219462445161703240957568807600494579282412972591613629025720312652350445062631757413159623885481128914333982571503540357043736821931054029305931122179293220911720263006705242490442826574028623201238659548887822088996956559517179003476743001815465428992906356931239533104
index 759b3b3ad35e11387221529673f5cb3b4ae7a15c..9dfe83dae5e7c2ec946d01109067b435c0393349 100644 (file)
@@ -3,12 +3,75 @@
               [adventofcode2019.intcode :as i]
               [clojure.string :as str]
               [clojure.core.match :refer [match]]
-              [clojure.math.combinatorics :as combo]])
+              [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))
+        min-x (reduce min x-list)
+        max-x (reduce max x-list)
+        min-y (reduce min y-list)
+        max-y (reduce max y-list)]
+    [[min-x max-x] [min-y max-y]]))
+
+(defn make-field [[dx dy]]
+  (let [line (vec (repeat dx \space))]
+    (mapv (constantly line) (range dy))))
+
+(defn draw-tiles [tiles term]
+  (let [to-text #(case %
+                   0 \space 
+                   1 \|
+                   2 \-
+                   3 \_
+                   4 \O)
+        paint-tile (fn [[[x y] tile]]
+                       (ts/move-cursor term x y)
+                       (ts/put-character term (to-text tile)))]
+    (doall (map paint-tile tiles))))
+
+(defn print-field [field score]
+  (run! (comp println str/join) field)
+  (println score)
+  (flush))
+
+(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)
+    (loop [program-state (get-next-ic-output new-game)
+           score 0
+           framerate 0
+           ctr 0]
+      (Thread/sleep framerate)
+      (if (:exit program-state)
+        (do (ts/stop field)
+            score)
+        (let [[x y t] (:output program-state)
+              [new-score new-screen] (if (and (= x -1) (= y 0))
+                                         [t {}]
+                                         [score {[x y] t}])
+              input (case (ts/get-key-blocking field {:interval 1 :timeout framerate})
+                      \a -1
+                      \d 1
+                      0)]
+          (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)))))))
 
 (defn day13 []
   (let [input (i/get-program (input-file))
         output (:output (i/intcode (i/build-state input)))
         draw-tiles (fn [screen [x y t]] (assoc screen [x y] t)) 
-        screen (reduce draw-tiles {} (partition 3 output))] 
-    (part1 (count (filter #(= % 4) (vals screen))))
-    #_(part2)))
+        screen (reduce draw-tiles {} (partition 3 output))
+        game (assoc-in (i/build-state input {:step true}) [:memory 0] 2)] 
+    (part1 (count (filter #(= % 2) (vals screen))))
+    (part2 (run-game game (find-bounds screen)))))
index 9f09f1083f4a9fcff5c7e3f2994fdc86abdf2017..d3e010d534591a54f9a4cad40cdedc9cedfc1a50 100644 (file)
@@ -62,7 +62,7 @@
     (apply operation state args)))
 
 (defn get-program [input-file]
-  (map parse-int (get-list-from-file input-file) #","))
+  (map parse-int (get-list-from-file input-file #",")))
 
 (defn build-state 
   ([program]