summaryrefslogtreecommitdiff
path: root/9-2.scm
diff options
context:
space:
mode:
authorlamp2023-12-11 14:07:09 +0000
committerlamp2023-12-11 14:07:09 +0000
commitcd6a52f02a3c3d7b8e2565c3199174ba1625e899 (patch)
tree55e698dd73b765d41a31d44f708af8c41621deea /9-2.scm
parentd0842f0de1c0a3370ba3fe49918f664863bac100 (diff)
add day 9 solutions
Diffstat (limited to '9-2.scm')
-rwxr-xr-x9-2.scm31
1 files changed, 31 insertions, 0 deletions
diff --git a/9-2.scm b/9-2.scm
new file mode 100755
index 0000000..fe3f073
--- /dev/null
+++ b/9-2.scm
@@ -0,0 +1,31 @@
+#!/usr/bin/env -S guile -s
+!#
+(use-modules (ice-9 textual-ports)
+ (srfi srfi-1))
+
+(define (read-lines port)
+ (letrec ((loop (lambda (l ls)
+ (if (eof-object? l)
+ ls
+ (loop (get-line port) (cons l ls))))))
+ (reverse (loop (get-line port) '()))))
+
+(define (split-up nums)
+ (map string->number (filter (lambda (x) (not (string-null? x))) (string-split nums char-set:whitespace))))
+
+(define (seq-diff seq)
+ (map (lambda (x y) (- y x)) (drop-right seq 1) (cdr seq)))
+
+(define (all-seq-diffs seq)
+ (letrec ((loop (lambda (l ls)
+ (if (every zero? l) ls
+ (loop (seq-diff l) (cons l ls))))))
+ (loop seq '())))
+
+(define (predict seq-diffs)
+ (fold - 0 (map car seq-diffs)))
+
+(let* ((port (open-input-file "9.txt"))
+ (lines (map split-up (read-lines port))))
+ (close-port port)
+ (display (fold + 0 (map (lambda (x) (predict (all-seq-diffs x))) lines))))