diff options
author | Prefetch | 2024-03-02 19:36:12 +0100 |
---|---|---|
committer | Prefetch | 2024-03-02 19:36:12 +0100 |
commit | 1fbb07c54523c7a576bfff1cb689e155dd55f15a (patch) | |
tree | 7aa9f92a7d99ae9203b538803b7efefd846b67e0 /01/lib.scm | |
parent | af589b238c1d51960d8af3b36041aca2bad7855b (diff) |
Add first five days
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))) + +) |