Skip to content

Commit cbc2e48

Browse files
committed
Day 17: Clojure (Part 1)
1 parent 7a5d0c2 commit cbc2e48

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

17-clojure/input/input

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target area: x=195..238, y=-93..-67

17-clojure/input/simple

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target area: x=20..30, y=-10..-5

17-clojure/main.clj

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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)))

0 commit comments

Comments
 (0)