aboutsummaryrefslogtreecommitdiff
path: root/03/1.scm
diff options
context:
space:
mode:
Diffstat (limited to '03/1.scm')
-rw-r--r--03/1.scm27
1 files changed, 27 insertions, 0 deletions
diff --git a/03/1.scm b/03/1.scm
new file mode 100644
index 0000000..b921dba
--- /dev/null
+++ b/03/1.scm
@@ -0,0 +1,27 @@
+(import (chicken io))
+(import regex)
+(import (chicken string))
+
+(define in-str
+ (with-input-from-file "input" read-string))
+
+;; (display in-str)
+(define re "mul\\([0-9]+,[0-9]+\\)")
+
+(define matched
+ (let loop ((start 0) (result '()))
+ (define search-ret (string-search re in-str start))
+ (if (not search-ret)
+ (reverse result)
+ (loop (cadar (string-search-positions re in-str start))
+ (cons (car search-ret) result)))))
+
+(define (extract-numbers str)
+ (define nums-str (substring str 4 (- (string-length str) 1)))
+ (map string->number (string-split nums-str ",")))
+
+(define (calculate str)
+ (define vals (extract-numbers str))
+ (* (car vals) (cadr vals)))
+
+(display (apply + (map calculate matched)))