diff options
Diffstat (limited to '01/lib.scm')
-rw-r--r-- | 01/lib.scm | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/01/lib.scm b/01/lib.scm new file mode 100644 index 0000000..fcdf642 --- /dev/null +++ b/01/lib.scm @@ -0,0 +1,34 @@ +(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))) + +) |