diff options
Diffstat (limited to '05/lib.scm')
-rw-r--r-- | 05/lib.scm | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/05/lib.scm b/05/lib.scm new file mode 100644 index 0000000..9a9951b --- /dev/null +++ b/05/lib.scm @@ -0,0 +1,29 @@ +(library (lib) + (export solve-part1 solve-part2) + (import (chezscheme)) + + ; Follow the jump instructions until we end up outside, + ; updating every offset we touch according to `proc' + (define (solve-puzzle proc lines) + (define code (list->vector (map string->number lines))) + (let loop ((i 0) (count 0)) + (if (or (< i 0) (>= i (vector-length code))) + count + (let ((offset (vector-ref code i))) + (vector-set! code i (proc offset)) + (loop (+ i offset) (+ count 1)))))) + + (define (solve-part1 lines) + (solve-puzzle + (lambda (offset) (+ offset 1)) + lines)) + + (define (solve-part2 lines) + (solve-puzzle + (lambda (offset) + (if (>= offset 3) + (- offset 1) + (+ offset 1))) + lines)) + +) |