aboutsummaryrefslogtreecommitdiff
path: root/03/1.scm
blob: b921dba3e381e9c706bd422249225f9f181dcea8 (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
(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)))