]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
day2: cleaner approach
authorClyne Sullivan <clyne@bitgloo.com>
Thu, 2 Dec 2021 19:39:09 +0000 (14:39 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Thu, 2 Dec 2021 19:39:09 +0000 (14:39 -0500)
day2/part1.clj
day2/part2.clj

index 1a4df6b0f3528cb5cf3228dc85c0c436296a2dad..6b092127d10720a00951df579ab40c79907c34cd 100644 (file)
@@ -8,22 +8,18 @@
 
 (require '[clojure.string :as str])
 
-(loop [xpos 0 depth 0]
+(loop [data {:xpos 0 :depth 0}]
   (let [input (read-line)]
     (if (empty? input)
-      (println (* xpos depth))
+      (println (apply * (vals data)))
       (let [ins (str/split input #" ")
             n (Integer/parseInt (second ins))
             ]
         (recur
-          (if (= (first ins) "forward")
-            (+ xpos n)
-            xpos
-            )
           (case (first ins)
-            "up" (- depth n)
-            "down" (+ depth n)
-            depth
+            "forward" (update-in data [:xpos] + n)
+            "up" (update-in data [:depth] - n)
+            "down" (update-in data [:depth] + n)
             )
           )
         )
index 35fe3fb059dadc5fd2c3bd6f8a16bfa4033f994c..7eeeb214342a476a5556aca9c703329bfbb7d342 100644 (file)
@@ -8,26 +8,22 @@
 
 (require '[clojure.string :as str])
 
-(loop [xpos 0 depth 0 aim 0]
+(loop [data {:xpos 0 :depth 0 :aim 0}]
   (let [input (read-line)]
     (if (empty? input)
-      (println (* xpos depth))
+      (println (apply * (map data [:xpos :depth])))
       (let [ins (str/split input #" ")
             n (Integer/parseInt (second ins))
             ]
         (recur
-          (if (= (first ins) "forward")
-            (+ xpos n)
-            xpos
-            )
-          (if (= (first ins) "forward")
-            (+ depth (* aim n))
-            depth
-            )
           (case (first ins)
-            "down" (+ aim n)
-            "up" (- aim n)
-            aim)
+            "forward" (-> data
+                          (update-in [:xpos] + n)
+                          (update-in [:depth] + (* (data :aim) n))
+                          )
+            "up" (update-in data [:aim] - n)
+            "down" (update-in data [:aim] + n)
+            )
           )
         )
       )