(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)) )