|
|
|
#!/usr/bin/guile -s
|
|
|
|
!#
|
|
|
|
|
|
|
|
(use-modules (ice-9 rdelim))
|
|
|
|
|
|
|
|
(define (read-lines)
|
|
|
|
(let loop ((line (read-line)))
|
|
|
|
(if
|
|
|
|
(eof-object? line)
|
|
|
|
'()
|
|
|
|
(cons line (loop (read-line))))))
|
|
|
|
|
|
|
|
(define (map list mapper)
|
|
|
|
(if
|
|
|
|
(null? list)
|
|
|
|
'()
|
|
|
|
(cons (mapper (car list)) (map (cdr list) mapper))))
|
|
|
|
|
|
|
|
(define (reduce list reducer current)
|
|
|
|
(if
|
|
|
|
(null? list)
|
|
|
|
current
|
|
|
|
(reduce (cdr list) reducer (reducer current (car list)))))
|
|
|
|
|
|
|
|
(define (combine list combiner)
|
|
|
|
(if
|
|
|
|
(null? (car list))
|
|
|
|
(cdr list)
|
|
|
|
(if
|
|
|
|
(null? (cdr list))
|
|
|
|
(car list)
|
|
|
|
(combiner (car list) (cdr list)))))
|
|
|
|
|
|
|
|
(define (sum numbers) (reduce numbers + 0))
|
|
|
|
|
|
|
|
(define (is-numeric-char char) (if (char-numeric? char) #t #f))
|
|
|
|
|
|
|
|
(define (first-last list predicate)
|
|
|
|
(if
|
|
|
|
(null? list)
|
|
|
|
'()
|
|
|
|
(combine
|
|
|
|
(cons
|
|
|
|
(if
|
|
|
|
(predicate (car list))
|
|
|
|
(cons (car list) (cons (car list) '()))
|
|
|
|
'())
|
|
|
|
(first-last (cdr list) predicate))
|
|
|
|
(lambda (current rest)
|
|
|
|
(cons (car current) (cdr rest))))))
|
|
|
|
|
|
|
|
(define (solve-line line)
|
|
|
|
(
|
|
|
|
(lambda (first-last-result) (string->number (list->string first-last-result)))
|
|
|
|
(first-last (string->list line) is-numeric-char)))
|
|
|
|
|
|
|
|
(define (solve-all lines)
|
|
|
|
(sum (map lines solve-line)))
|
|
|
|
|
|
|
|
(display (solve-all (read-lines)))
|