From a9def511da1cf7c7005324f616ac2f9dcf812d3c Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sun, 3 Dec 2017 12:36:27 -0800 Subject: [PATCH] d3 --- 2017/d3/main.rkt | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 2017/d3/star1.rkt | 2 ++ 2017/d3/star2.rkt | 2 ++ 2017/d3/test1.rkt | 5 +++++ 2017/d3/test2.rkt | 5 +++++ 5 files changed, 63 insertions(+) create mode 100644 2017/d3/main.rkt create mode 100644 2017/d3/star1.rkt create mode 100644 2017/d3/star2.rkt create mode 100644 2017/d3/test1.rkt create mode 100644 2017/d3/test2.rkt diff --git a/2017/d3/main.rkt b/2017/d3/main.rkt new file mode 100644 index 0000000..43bac57 --- /dev/null +++ b/2017/d3/main.rkt @@ -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))) \ No newline at end of file diff --git a/2017/d3/star1.rkt b/2017/d3/star1.rkt new file mode 100644 index 0000000..210eb1e --- /dev/null +++ b/2017/d3/star1.rkt @@ -0,0 +1,2 @@ +#lang reader "main.rkt" ★ +347991 \ No newline at end of file diff --git a/2017/d3/star2.rkt b/2017/d3/star2.rkt new file mode 100644 index 0000000..63814af --- /dev/null +++ b/2017/d3/star2.rkt @@ -0,0 +1,2 @@ +#lang reader "main.rkt" ★★ +347991 \ No newline at end of file diff --git a/2017/d3/test1.rkt b/2017/d3/test1.rkt new file mode 100644 index 0000000..970116a --- /dev/null +++ b/2017/d3/test1.rkt @@ -0,0 +1,5 @@ +#lang reader "main.rkt" ★ +1 +12 +23 +1024 \ No newline at end of file diff --git a/2017/d3/test2.rkt b/2017/d3/test2.rkt new file mode 100644 index 0000000..20e635d --- /dev/null +++ b/2017/d3/test2.rkt @@ -0,0 +1,5 @@ +#lang reader "main.rkt" ★★ +1 +12 +23 +1024 \ No newline at end of file