From 75b2595299f0602142e5833b841702c8d0ac94af Mon Sep 17 00:00:00 2001 From: mryouse Date: Tue, 24 May 2022 01:40:05 +0000 Subject: add a roundabout functional solution --- aoc/day01/part1.neb | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'aoc/day01/part1.neb') diff --git a/aoc/day01/part1.neb b/aoc/day01/part1.neb index 80a9fc8..76f949c 100644 --- a/aoc/day01/part1.neb +++ b/aoc/day01/part1.neb @@ -18,20 +18,22 @@ (redef prev _item_)) acc) +; (a b c d e) => ((a b) (b c) (c d) (d e)) (func pairwise (inp) (zip (list-reverse (rest (list-reverse inp))) (rest inp))) -; what i really need to get working is 'apply +(func abs (num) + (if (< num 0) + (* (- 0 1) num) + num)) + (func list-subtract (lst) - (- (first lst) (list-subtract (rest lst)))) + (def ret (apply - lst)) + (- (/ (abs ret) ret) 1)) +; this algorithm is very roundabout (func increase-count-iter-2 (inp) - ;(print (->string (list-reverse (rest (list-reverse inp))))) - ;(print (->string (rest inp))) - (def pairwise (zip (list-reverse (rest (list-reverse inp))) (rest inp))) - (print (->string pairwise)) - ) - + (abs (/ (apply + (map list-subtract (pairwise inp))) 2))) (def lines (map string->int @@ -39,5 +41,5 @@ (read-lines "input.txt")))) ;(print (->string (increase-count-recur lines 0))) -;(print (->string (increase-count-iter lines 0))) -(print (->string (increase-count-iter-2 lines 0))) +(print (->string (increase-count-iter lines))) +(print (->string (increase-count-iter-2 lines))) -- cgit v1.2.3