[ prog / sol / mona ]

prog


LISP Puzzles

125 2020-05-20 20:27 *

>>123,124
Nice. I also just realized that now I have no reason to save the R values. Their queue has been removed.

(use-modules (ice-9 q))

(define (sequence n)
  (let* ((rs 7)                         ; Smallest R value unused.
         (ss (make-q))                  ; Queue of S values.
         (r 7)                          ; Current R value.
         (s 5)                          ; Current S value.
         (negsum -7)                    ; Negative sum of unused R values.
         (pos 3)                        ; Smallest n in the queues.
         (n (- n 1)))                   ; Off by one.
    (enq! ss s)
    (let loop ((i 4))
      (when (<= (+ s r) (+ i n))
        (set! r (+ r s))
        (set! s (+ s 1))
        (set! negsum (- negsum r))
        (when (= s rs)
          (set! rs (+ rs (q-front ss)))
          (set! negsum (+ negsum s))
          (set! s (+ s 1))
          (set! pos (+ pos 1))
          (deq! ss))
        (enq! ss s)
        (loop (+ i 1))))
    (list pos negsum r (cadr ss))))

(define (sum m n)
  (* (/ (- m n -1) 2) (+ m n)))

(define (jump n)
  (cond
   ((= n 1) 1)
   ((= n 2) 3)
   ((= n 3) 7)
   ((= n 4) 12)
   (else
    (let* ((seq (sequence n))
           (m   (list-ref seq 0))
           (ns  (list-ref seq 1))
           (r'  (list-ref seq 2))
           (s'  (list-ref seq 3))
           (n'  (+ m -1))
           (s   (+ s' (- n n'))))
      (+ r' (sum (- s 1) s') ns)))))

This is my last update for today as it feels spammy. If you are going to rerun the benchmarks, please use this version.

157


VIP:

do not edit these