From 1fbb07c54523c7a576bfff1cb689e155dd55f15a Mon Sep 17 00:00:00 2001 From: Prefetch Date: Sat, 2 Mar 2024 19:36:12 +0100 Subject: Add first five days --- 05/lib.scm | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 05/lib.scm (limited to '05/lib.scm') 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)) + +) -- cgit v1.2.3