#!/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))))