no globals

master
Matthew Butterick 7 years ago
parent 6114504d73
commit 42b6c84b74

@ -9,36 +9,35 @@
datum) datum)
#,@(cdr lines)))) #,@(cdr lines))))
(define vec #f) (define-macro (#%mb STARS RANGE-IN STR)
(define (set-knot-range! int) (set! vec (list->vector (range int))))
(define-macro (#%mb STARS RANGE STR)
#`(#%module-begin #`(#%module-begin
(set-knot-range! RANGE) ((if (eq? 'STARS ') one-star two-star) RANGE-IN STR)))
((if (eq? 'STARS ') one-star two-star) STR)))
(define (one-star str) (define (one-star range-in str)
(define lens (for/list ([len (in-port read (open-input-string (string-replace str "," " ")))]) (define lens (with-input-from-string (string-replace str "," " ")
len)) (λ () (for/list ([len (in-port)])
(reverse-segments lens) len))))
(* (vector-ref vec 0) (vector-ref vec 1))) (define nums (reverse-segments range-in lens))
(* (first nums) (second nums)))
(define (two-star str) (define (two-star range-in str)
(define ascii-chars (map char->integer (string->list str))) (define ascii-chars (map char->integer (string->list str)))
(reverse-segments (append ascii-chars '(17 31 73 47 23)) #:count 64) (define nums (reverse-segments range-in (append ascii-chars '(17 31 73 47 23)) #:reps 64))
(define dense-hash (for/list ([vals (in-slice 16 (vector->list vec))]) (define dense-hash (for/list ([vals (in-slice 16 nums)])
(apply bitwise-xor vals))) (apply bitwise-xor vals)))
(string-append* (for/list ([num (in-list dense-hash)]) (string-append* (for/list ([num (in-list dense-hash)])
(~r num #:base 16 #:min-width 2 #:pad-string "0")))) (~r num #:base 16 #:min-width 2 #:pad-string "0"))))
(define (reverse-segments lens #:count [count 1]) (define (reverse-segments range-in lens #:reps [reps 1])
(define vec (list->vector (range range-in)))
(for*/fold ([current-position 0] (for*/fold ([current-position 0]
[skip-size 0]) [skip-size 0])
([i (in-range count)] ([rep (in-range reps)]
[len (in-list lens)]) [len (in-list lens)])
(define posns (for/list ([i (in-range len)]) (define posns (for/list ([i (in-range len)])
(modulo (+ current-position i) (vector-length vec)))) (modulo (+ current-position i) range-in)))
(for ([val (in-list (map (curry vector-ref vec) posns))] (for ([val (in-list (map (curry vector-ref vec) posns))]
[posn (in-list (reverse posns))]) [posn (in-list (reverse posns))])
(vector-set! vec posn val)) (vector-set! vec posn val))
(values (+ current-position len skip-size) (add1 skip-size)))) (values (+ current-position len skip-size) (add1 skip-size)))
(vector->list vec))