|
17 | 17 | (< vx 0) (+ vx 1)
|
18 | 18 | (> vx 0) (- vx 1)
|
19 | 19 | :else 0
|
20 |
| - ) |
| 20 | + ) |
21 | 21 | (- vy 1)))))
|
22 | 22 |
|
23 | 23 | (def ^:const DEBUG false)
|
24 | 24 | (def ^:const UNCHANGED_CNT 50)
|
25 | 25 |
|
26 | 26 | (def highest (atom [ -1 0 ]))
|
| 27 | +(def allHits (atom [])) |
27 | 28 | (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 ] |
31 | 31 | (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) ] |
37 | 37 | (while (> @unchanged 0)
|
38 | 38 | (if DEBUG (println "vy:" @vy))
|
39 | 39 | (let [ vx (atom 0) ]
|
40 | 40 | (swap! vx (vector 0))
|
41 |
| - (while (<= @vx tx1) |
| 41 | + (while (<= @vx tx2) |
42 | 42 | (if (traceShot tx1 ty1 tx2 ty2 0 0 @vx @vy)
|
43 | 43 | (do
|
44 | 44 | (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))) |
46 | 48 | (swap! unchanged (constantly UNCHANGED_CNT))))
|
47 | 49 | (swap! vx inc))
|
48 |
| - (swap! vy inc)) |
| 50 | + (swap! vy (fn [prev] (+ prev dvy))) |
49 | 51 |
|
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 | + ))) |
54 | 55 |
|
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