aboutsummaryrefslogtreecommitdiff
path: root/02/1.scm
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2024-12-02 18:22:34 +0800
committerMistivia <i@mistivia.com>2024-12-02 18:22:34 +0800
commit7f026cafe35eb5dbd79432cafbe0ad6f74386130 (patch)
tree207b47e70fca1789ae94a6a97b755de5dbb8f61d /02/1.scm
parenta4418ca7e5c192e9a438ae80097c6374e19341f6 (diff)
day 2
Diffstat (limited to '02/1.scm')
-rw-r--r--02/1.scm41
1 files changed, 41 insertions, 0 deletions
diff --git a/02/1.scm b/02/1.scm
new file mode 100644
index 0000000..2c5c9e0
--- /dev/null
+++ b/02/1.scm
@@ -0,0 +1,41 @@
+(import (chicken string))
+(import (chicken io))
+(import (chicken sort))
+(import matchable)
+(import srfi-1)
+
+(define input
+ (with-input-from-file "input"
+ (lambda ()
+ (let loop ((ret '()))
+ (let ((line (read-line)))
+ (if (eof-object? line)
+ (reverse ret)
+ (loop (cons line ret))))))))
+
+(set! input (map string-split input))
+(set! input (map (lambda (lst) (map string->number lst)) input))
+
+(define (diff in)
+ (let loop ((lst in) (ret '()))
+ (match lst
+ ((a b . r) (loop (cdr lst) (cons (- a b) ret)))
+ ((a) (reverse ret)))))
+
+(define ladder (map diff input))
+
+(define (same-sign in)
+ (define prod (map (lambda (x) (* x (car in))) in))
+ (foldl (lambda (a b) (and a b)) #t (map (lambda (x) (> x 0)) prod)))
+
+(define (safe in)
+ (not (any (lambda (x) (> (abs x) 3)) in)))
+
+(define result
+ (map (lambda (a b) (and a b))
+ (map same-sign ladder)
+ (map safe ladder)))
+
+(display (length (filter (lambda (x) x) result)))
+(newline)
+