summaryrefslogtreecommitdiff
path: root/05/lib.scm
blob: 9a9951b72d7ccce4fe1c75c41a4cd7d4fd3e40d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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))

)