aboutsummaryrefslogtreecommitdiff
path: root/aoc/2021/day01/part1.neb
diff options
context:
space:
mode:
authormryouse2023-06-03 20:22:18 +0000
committermryouse2023-06-03 20:22:18 +0000
commit7b7bf727da3250907284368b98b0083a4b4e2386 (patch)
tree34802caea5181dc0fb97992746ab99b080448418 /aoc/2021/day01/part1.neb
initial commit
Diffstat (limited to 'aoc/2021/day01/part1.neb')
-rw-r--r--aoc/2021/day01/part1.neb55
1 files changed, 55 insertions, 0 deletions
diff --git a/aoc/2021/day01/part1.neb b/aoc/2021/day01/part1.neb
new file mode 100644
index 0000000..066a86d
--- /dev/null
+++ b/aoc/2021/day01/part1.neb
@@ -0,0 +1,55 @@
+;; https://adventofcode.com/2021/day/1
+
+; this feels more lisp-y
+; maximum recursion depth reached quickly (in neb-python)
+(func increase-count-recur (inp acc)
+ (branch
+ ((eq? 0 (length (rest inp))) acc)
+ ((> (first (rest inp)) (first inp))
+ (increase-count-recur (rest inp) (+ 1 acc)))
+ (#true
+ (increase-count-recur (rest inp) acc))))
+
+; this works, but i don't like redefining variables
+(func increase-count-iter (inp)
+ (def acc 0)
+ (def prev (first inp))
+ (for-each (rest inp)
+ (if (> _item_ prev)
+ (redef acc (+ 1 acc)))
+ (redef prev _item_))
+ acc)
+
+; (a b c d e) => ((a b) (b c) (c d) (d e))
+(func pairwise (inp)
+ (zip (most inp) (rest inp)))
+
+(func abs (num)
+ (if (< num 0)
+ (* (- 1) num)
+ num))
+
+(func list-subtract (lst)
+ (def ret (apply - lst))
+ (- (/ (abs ret) ret) 1))
+
+; this algorithm is very roundabout
+(func increase-count-iter-2 (inp)
+ (abs (/ (apply + (map list-subtract (pairwise inp))) 2)))
+
+; this is the most ideomatic
+(func increase-count (inp)
+ (length
+ (filter
+ (lambda (x) (apply < x))
+ (pairwise inp))))
+
+(def lines
+ (map string->int
+ (map strip
+ (read-lines "input.txt"))))
+
+;(print (->string (increase-count-recur lines 0)))
+(print (->string (increase-count-iter lines)))
+(print (->string (increase-count-iter-2 lines)))
+(print (->string (increase-count lines)))