puzzle_6.scm (1155B)
1 (define (distinct elements) 2 (define (count-distinct l d count) 3 (cond ((null? l) count) 4 ((member (car l) d) 5 (count-distinct (cdr l) d count)) 6 (else 7 (count-distinct (cdr l) (cons (car l) d) (+ 1 count))))) 8 (count-distinct elements '() 0)) 9 10 (define (right-fold answers policy) 11 (if (not (null? answers)) 12 (if (not (= (string-length (car answers)) 0)) 13 (append (policy (car answers)) (right-fold (cdr answers) policy)) 14 (list (right-fold (cdr answers) policy))) 15 '())) 16 17 (define (unfold answers) 18 (if (not (null? answers)) 19 (let ((tail (reverse answers))) 20 (if (not (pair? (car tail))) 21 (list tail) 22 (cons (cdr tail) (unfold (car tail))) 23 )) 24 '())) 25 26 (load "read_lines.scm") 27 (define answers (read-lines "files/6.txt" (lambda (x) x))) 28 29 (display (apply + (map distinct (unfold (right-fold answers string->list))))) (newline) 30 (display (apply + (map distinct (map (lambda (sets) (if (pair? (car sets)) (apply lset-intersection eqv? sets) '())) 31 (unfold (right-fold answers (lambda (x) (list (string->list x))))))))) (newline)