File tree Expand file tree Collapse file tree 3 files changed +58
-0
lines changed Expand file tree Collapse file tree 3 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ target area: x=195..238, y=-93..-67
Original file line number Diff line number Diff line change 1+ target area: x=20..30, y=-10..-5
Original file line number Diff line number Diff line change 1+ (if (not (== (count *command-line-args*) 2 ))
2+ (do
3+ (println " incorrect number of arguments" )
4+ (System/exit 1 )))
5+
6+ (defn traceShot
7+ [tx1 ty1 tx2 ty2 x y vx vy]
8+ (do
9+ (cond
10+ (or (> x tx2) (< y ty2)) false
11+ (and (>= x tx1) (<= y ty1)) true
12+ :else (recur
13+ tx1 ty1 tx2 ty2
14+ (+ x vx)
15+ (+ y vy)
16+ (cond
17+ (< vx 0 ) (+ vx 1 )
18+ (> vx 0 ) (- vx 1 )
19+ :else 0
20+ )
21+ (- vy 1 )))))
22+
23+ (def ^:const DEBUG false )
24+ (def ^:const UNCHANGED_CNT 50 )
25+
26+ (def highest (atom [ -1 0 ]))
27+ (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+ ]
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) ]
37+ (while (> @unchanged 0 )
38+ (if DEBUG (println " vy:" @vy))
39+ (let [ vx (atom 0 ) ]
40+ (swap! vx (vector 0 ))
41+ (while (<= @vx tx1)
42+ (if (traceShot tx1 ty1 tx2 ty2 0 0 @vx @vy)
43+ (do
44+ (if DEBUG (println " found vx:" @vx " vy:" @vy))
45+ (swap! highest (constantly [ @vx @vy ]))
46+ (swap! unchanged (constantly UNCHANGED_CNT))))
47+ (swap! vx inc))
48+ (swap! vy inc))
49+
50+ (if (> @vy 0 ) (swap! unchanged dec))
51+ (if DEBUG (println @unchanged " unchanged left" )))
52+ )
53+ )
54+
55+ (println " best pair:" @highest)
56+ (println " largest height:" (* (/ (second @highest) 2 ) (+ (second @highest) 1 )))
You can’t perform that action at this time.
0 commit comments