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