diff --git a/day01-easy/main.scm b/day01-easy/main.scm index 8a2f0ad..24b473c 100755 --- a/day01-easy/main.scm +++ b/day01-easy/main.scm @@ -22,14 +22,14 @@ current (reduce (cdr list) reducer (reducer current (car list))))) -(define (combine list combiner) +(define (combine a b combiner) (if - (null? (car list)) - (cdr list) + (null? a) + b (if - (null? (cdr list)) - (car list) - (combiner (car list) (cdr list))))) + (null? b) + a + (combiner a b)))) (define (sum numbers) (reduce numbers + 0)) @@ -43,22 +43,20 @@ (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)) - (repeat (car list) 2) - '()) - (first-last (cdr list) predicate)) - (lambda (current rest) - (cons (car current) (cdr rest)))))) + (reduce + (map list (lambda (entry) + (if (predicate entry) (repeat entry 2) '()))) + (lambda (current accumulator) + (combine + current + accumulator + (lambda (current rest) + (cons (car current) (cdr rest))))) + '())) (define (solve-line line) - ( - (lambda (first-last-result) (string->number (list->string first-last-result))) + ((lambda (first-last-result) + (string->number (list->string first-last-result))) (first-last (string->list line) is-numeric-char))) (define (solve-all lines)