summaryrefslogtreecommitdiff
path: root/1.scm
blob: 43ecdba0edab98af51df91a369f05e15f3321606 (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/env -S 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))))