commit 2e3d77d050ca0396d4e9ab2d8014b3f3d6dee440 parent 7208510688829d5094997145c51eb9020558f6be Author: mpizzzle <m@michaelpercival.xyz> Date: Sun, 6 Dec 2020 19:45:21 +0000 puzzle 6 part 2 complete (fuck scheme) Diffstat:
| M | 2020/puzzle_6.scm | | | 12 | +++++++----- |
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/2020/puzzle_6.scm b/2020/puzzle_6.scm @@ -7,17 +7,17 @@ (count-distinct (cdr l) (cons (car l) d) (+ 1 count))))) (count-distinct elements '() 0)) -(define (load-answers answers) +(define (right-fold answers policy) (if (not (null? answers)) (if (not (= (string-length (car answers)) 0)) - (append (string->list (car answers)) (load-answers (cdr answers))) - (list (load-answers (cdr answers)))) + (append (policy (car answers)) (right-fold (cdr answers) policy)) + (list (right-fold (cdr answers) policy))) '())) (define (unfold answers) (if (not (null? answers)) (let ((tail (reverse answers))) - (if (char? (car tail)) + (if (not (pair? (car tail))) (list tail) (cons (cdr tail) (unfold (car tail))) )) @@ -26,4 +26,6 @@ (load "read_lines.scm") (define answers (read-lines "files/6.txt" (lambda (x) x))) -(display (apply + (map distinct (unfold (load-answers answers))))) (newline) +(display (apply + (map distinct (unfold (right-fold answers string->list))))) (newline) +(display (apply + (map distinct (map (lambda (sets) (if (pair? (car sets)) (apply lset-intersection eqv? sets) '())) + (unfold (right-fold answers (lambda (x) (list (string->list x))))))))) (newline)