summaryrefslogtreecommitdiff
path: root/1.scm
diff options
context:
space:
mode:
authorlamp2023-12-02 10:47:08 +0000
committerlamp2023-12-02 10:47:08 +0000
commit3af6a4012dd82adabf0e2ce9df886438b254e500 (patch)
tree9f8a58ff8cbf60b379dd913589799169f199dc3d /1.scm
add day 1 solutions
Diffstat (limited to '1.scm')
-rwxr-xr-x1.scm22
1 files changed, 22 insertions, 0 deletions
diff --git a/1.scm b/1.scm
new file mode 100755
index 0000000..7a6e953
--- /dev/null
+++ b/1.scm
@@ -0,0 +1,22 @@
+#!/usr/bin/guile -s
+!#
+(use-modules (ice-9 regex)
+ (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 (fix-line line)
+ (let ((matches (string-match "[a-z]*([0-9])(.*([0-9]))?" line)))
+ (string->number (string-append (match:substring matches 1)
+ (or (match:substring matches 3) (match:substring matches 1))))))
+
+(let* ((port (open-input-file "1.txt"))
+ (lines (read-lines port)))
+ (close-port port)
+ (display (fold + 0 (map fix-line lines))))