]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
add day 2
authorClyne Sullivan <clyne@bitgloo.com>
Thu, 2 Dec 2021 13:03:17 +0000 (08:03 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Thu, 2 Dec 2021 13:03:17 +0000 (08:03 -0500)
day2/part1.clj [new file with mode: 0644]
day2/part2.clj [new file with mode: 0644]

diff --git a/day2/part1.clj b/day2/part1.clj
new file mode 100644 (file)
index 0000000..1a4df6b
--- /dev/null
@@ -0,0 +1,33 @@
+; Day 2, part 2
+; Read a list of instructions from stdin:
+;   "down X" increases depth number by X,
+;   "up X" decreases depth by X,
+;   "forward X" increases xpos by X.
+; Print (xpos * depth) after end of data.
+;
+
+(require '[clojure.string :as str])
+
+(loop [xpos 0 depth 0]
+  (let [input (read-line)]
+    (if (empty? input)
+      (println (* xpos depth))
+      (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
+            )
+          )
+        )
+      )
+    )
+  )
+
diff --git a/day2/part2.clj b/day2/part2.clj
new file mode 100644 (file)
index 0000000..35fe3fb
--- /dev/null
@@ -0,0 +1,36 @@
+; Day 2, part 2
+; Read a list of instructions from stdin:
+;   "down X" increases aim number by X,
+;   "up X" decreases aim by X,
+;   "forward X" increases xpos by X and depth by (aim * X).
+; Print (xpos * depth) after end of data.
+;
+
+(require '[clojure.string :as str])
+
+(loop [xpos 0 depth 0 aim 0]
+  (let [input (read-line)]
+    (if (empty? input)
+      (println (* 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)
+          )
+        )
+      )
+    )
+  )
+