(library (lib) (export solve-part1 solve-part2) (import (chezscheme)) ; Rotate contents of `ilist' to the right `times' times (define (list-rotate ilist times) (let loop ((xs ilist) (n times)) (if (= n 0) xs (loop (append (cdr xs) (list (car xs))) (- n 1))))) (define (solve-puzzle chars chars-rot) (fold-left (lambda (accum c1 c2) ; Does `k'th char in list match with `k'th char in rotated list? (if (char=? c1 c2) ; Assume `c1' is a digit, convert it to integer in [0,9] (+ accum (string->number (string c1))) accum)) 0 chars chars-rot)) ; Compare each char to its right neighbour in circular list (define (solve-part1 str) (let* ((chars (string->list str)) (chars-rot (list-rotate chars 1))) (solve-puzzle chars chars-rot))) ; Compare each char to char halfway across circular list (define (solve-part2 str) (let* ((chars (string->list str)) (chars-rot (list-rotate chars (div (length chars) 2)))) (solve-puzzle chars chars-rot))) )