blob: 066a86d897caddf9c6e5b73092bfbd4c9bb77043 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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)))
|