summaryrefslogtreecommitdiff
path: root/1.scm
blob: 7a6e953f5b79323eb929fc43a7f3a950773e30a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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))))