|
|
|
#!/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 (id value) value)
|
|
|
|
|
|
|
|
(define (compose result-transformer f argument-transformer)
|
|
|
|
(lambda (value)
|
|
|
|
(result-transformer (f (argument-transformer value)))))
|
|
|
|
|
|
|
|
(define Y (lambda (f) (f (lambda (x) ((Y f) x)))))
|
|
|
|
|
|
|
|
(define (reduce-right reducer initial) (Y
|
|
|
|
(lambda (f) (lambda (list)
|
|
|
|
(if (null? list) initial (reducer (car list) (f (cdr list))))))))
|
|
|
|
|
|
|
|
(define (map mapper)
|
|
|
|
(reduce-right
|
|
|
|
(lambda (current accumulator) (cons (mapper current) accumulator))
|
|
|
|
'()))
|
|
|
|
|
|
|
|
(define (combine combiner)
|
|
|
|
(lambda (a b) (if (null? a) b (if (null? b) a (combiner a b)))))
|
|
|
|
|
|
|
|
(define sum (reduce-right + 0))
|
|
|
|
|
|
|
|
(define (repeat value) (Y
|
|
|
|
(lambda (f) (lambda (n)
|
|
|
|
(if (= n 0) '() (cons value (f (- n 1))))))))
|
|
|
|
|
|
|
|
(define (is-numeric-char char) (if (char-numeric? char) #t #f))
|
|
|
|
|
|
|
|
(define (first-last predicate) (compose
|
|
|
|
id
|
|
|
|
(reduce-right
|
|
|
|
(combine (lambda (left right) (cons (car left) (cdr right))))
|
|
|
|
'())
|
|
|
|
(map (lambda (entry) (if (predicate entry) ((repeat entry) 2) '())))))
|
|
|
|
|
|
|
|
(define solve-line (compose
|
|
|
|
(lambda (first-last-result) (string->number (list->string first-last-result)))
|
|
|
|
(first-last is-numeric-char)
|
|
|
|
string->list))
|
|
|
|
|
|
|
|
(define (solve-all lines) (sum ((map solve-line) lines)))
|
|
|
|
|
|
|
|
(display (solve-all (read-lines)))
|