]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
add day 1
authorClyne Sullivan <clyne@bitgloo.com>
Wed, 1 Dec 2021 22:19:58 +0000 (17:19 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Wed, 1 Dec 2021 22:19:58 +0000 (17:19 -0500)
day1/part1.clj [new file with mode: 0644]
day1/part2.clj [new file with mode: 0644]

diff --git a/day1/part1.clj b/day1/part1.clj
new file mode 100644 (file)
index 0000000..3d6a0f9
--- /dev/null
@@ -0,0 +1,22 @@
+; Day 1, part 1
+; Read a list of numbers from stdin, separated by newlines.
+; Count occurances of the current number being greater than
+; the previous.
+;
+
+(loop [inc-count 0
+       prev (Integer/parseInt (read-line))
+       ]
+  (let [input (read-line)]
+    (if (not (empty? input))
+      (let [depth (Integer/parseInt input)]
+        (recur
+          (if (> depth prev) (inc inc-count) inc-count)
+          depth
+          )
+        )
+      (println inc-count)
+      )
+    )
+  )
+
diff --git a/day1/part2.clj b/day1/part2.clj
new file mode 100644 (file)
index 0000000..b972e11
--- /dev/null
@@ -0,0 +1,29 @@
+; Day 1, part 2
+; Read a list of numbers from stdin, separated by newlines.
+; Count occurances of the current sum being greater than
+; the previous, where a sum is that of the current number,
+; the previous number, and the next number.
+;
+
+(loop [inc-count 0
+       buff (repeat 4 (Integer/parseInt (read-line)))
+       ]
+  (let [next (read-line)
+        new-count (if (> (last buff) (first buff))
+                    (inc inc-count)
+                    inc-count
+                    )
+        ]
+    (if (empty? next)
+      (println new-count)
+      (recur
+        new-count
+        (concat
+          (rest buff)
+          [(Integer/parseInt next)]
+          )
+        )
+      )
+    )
+  )
+