master
Matthew Butterick 7 years ago
parent 29760f48ac
commit 4b8627131a

@ -0,0 +1,49 @@
#lang sugar/debug br/quicklang
(require "../helper.rkt" sugar/cache)
(provide read-syntax)
(define (read-syntax path port)
(strip-context #`(module mod "main.rkt"
#,@(for/list ([datums (in-port read port)])
datums))))
(provide (rename-out [#%mb #%module-begin]))
(define-macro (#%mb STARS NUMBER ...)
#'(#%module-begin ((if (eq? 'STARS ') dist larger-sum) NUMBER) ...))
(define (ring-side r) (* 2 r))
(define (ring-last r) (expt (add1 (ring-side r)) 2))
(define (ring-first r) (if (zero? r) 1 (add1 (ring-last (sub1 r)))))
(define (ring int)
(for/first ([i (in-naturals)]
#:when (<= int (ring-last i)))
i))
(define (nth-coordinate n)
(cond
[(= n 1) 0]
[else
(define ring-idx (ring n))
(define offset (- n (ring-first ring-idx)))
(define-values (quadrant pos)
(quotient/remainder offset (ring-side ring-idx)))
(* (+ ring-idx (* +i (- pos (sub1 ring-idx)))) (expt +i quadrant))]))
(define (dist n)
(define c (nth-coordinate n))
(+ (abs (real-part c)) (abs (imag-part c))))
(define vals (make-hash))
(define (neighbor-sum n)
(define c (nth-coordinate n))
(define neighbor-offsets '(1 1+i +i -1+i -1 -1-1i -i 1-i))
(hash-ref! vals c (λ () (if (= c 0)
1
(for/sum ([n (in-list neighbor-offsets)])
(hash-ref vals (+ c n) 0))))))
(define (larger-sum x)
(for*/first ([n (in-naturals 1)]
#:when (> (neighbor-sum n) x))
(neighbor-sum n)))

@ -0,0 +1,2 @@
#lang reader "main.rkt"
347991

@ -0,0 +1,2 @@
#lang reader "main.rkt" ★★
347991

@ -0,0 +1,5 @@
#lang reader "main.rkt"
1
12
23
1024

@ -0,0 +1,5 @@
#lang reader "main.rkt" ★★
1
12
23
1024