Skip to content

Commit 4a5c285

Browse files
committed
Day 17: Clojure (Part 2)
1 parent cbc2e48 commit 4a5c285

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

17-clojure/main.clj

+31-18
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,53 @@
1717
(< vx 0) (+ vx 1)
1818
(> vx 0) (- vx 1)
1919
:else 0
20-
)
20+
)
2121
(- vy 1)))))
2222

2323
(def ^:const DEBUG false)
2424
(def ^:const UNCHANGED_CNT 50)
2525

2626
(def highest (atom [ -1 0 ]))
27+
(def allHits (atom []))
2728
(def fileContent (slurp (last *command-line-args*)))
28-
(let [ [ _ g1 g2 g3 g4 ]
29-
(map read-string (re-find #"target area: x=(-?\d+)\.\.(-?\d+), y=(-?\d+)\.\.(-?\d+)\s*" fileContent))
30-
]
29+
30+
(defn findHits [ vy0 dvy _tx1 _tx2 _ty1 _ty2 ]
3131
(let [ unchanged (atom UNCHANGED_CNT)
32-
tx1 (min g1 g2)
33-
tx2 (max g1 g2)
34-
ty1 (max g3 g4)
35-
ty2 (min g3 g4)
36-
vy (atom ty1) ]
32+
tx1 (min _tx1 _tx2)
33+
tx2 (max _tx1 _tx2)
34+
ty1 (max _ty1 _ty2)
35+
ty2 (min _ty1 _ty2)
36+
vy (atom vy0) ]
3737
(while (> @unchanged 0)
3838
(if DEBUG (println "vy:" @vy))
3939
(let [ vx (atom 0) ]
4040
(swap! vx (vector 0))
41-
(while (<= @vx tx1)
41+
(while (<= @vx tx2)
4242
(if (traceShot tx1 ty1 tx2 ty2 0 0 @vx @vy)
4343
(do
4444
(if DEBUG (println "found vx:" @vx "vy:" @vy))
45-
(swap! highest (constantly [ @vx @vy ]))
45+
(let [ pair [ @vx @vy ] ]
46+
(swap! allHits (fn [prev] (conj prev pair)))
47+
(swap! highest (constantly pair)))
4648
(swap! unchanged (constantly UNCHANGED_CNT))))
4749
(swap! vx inc))
48-
(swap! vy inc))
50+
(swap! vy (fn [prev] (+ prev dvy)))
4951

50-
(if (> @vy 0) (swap! unchanged dec))
51-
(if DEBUG (println @unchanged "unchanged left")))
52-
)
53-
)
52+
(swap! unchanged dec)
53+
(if DEBUG (println @unchanged "unchanged left")))
54+
)))
5455

55-
(println "best pair:" @highest)
56-
(println "largest height:" (* (/ (second @highest) 2) (+ (second @highest) 1)))
56+
(let [ [ _ g1 g2 g3 g4 ]
57+
(map read-string (re-find #"target area: x=(-?\d+)\.\.(-?\d+), y=(-?\d+)\.\.(-?\d+)\s*" fileContent)) ]
58+
(case (first *command-line-args*)
59+
"part01" (do
60+
(findHits g3 1 g1 g2 g3 g4)
61+
(println "best pair:" @highest)
62+
(println "largest height:" (* (/ (second @highest) 2) (+ (second @highest) 1))))
63+
"part02" (let [ ymin (min g3 g4) ]
64+
(findHits ymin -1 g1 g2 g3 g4)
65+
(findHits (inc ymin) 1 g1 g2 g3 g4)
66+
(if DEBUG (println @allHits))
67+
(println "count of all matching velocities:" (count @allHits)))
68+
(println "unknown subcommand"))
69+
)

0 commit comments

Comments
 (0)