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 /05/lib.scm | |
parent | af589b238c1d51960d8af3b36041aca2bad7855b (diff) |
Add first five days
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)) + +) |