From d11b94c4038b16006ab8cd0522df3cd85450fdc4 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sat, 23 Dec 2017 23:08:51 -0800 Subject: [PATCH] d24 --- 2017/d24/main.rkt | 43 ++++++++++++++++++++++++++++++++++ 2017/d24/star1.rkt | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 2017/d24/star2.rkt | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 2017/d24/test1.rkt | 9 +++++++ 2017/d24/test2.rkt | 9 +++++++ 5 files changed, 177 insertions(+) create mode 100644 2017/d24/main.rkt create mode 100644 2017/d24/star1.rkt create mode 100644 2017/d24/star2.rkt create mode 100644 2017/d24/test1.rkt create mode 100644 2017/d24/test2.rkt diff --git a/2017/d24/main.rkt b/2017/d24/main.rkt new file mode 100644 index 0000000..627687b --- /dev/null +++ b/2017/d24/main.rkt @@ -0,0 +1,43 @@ +#lang br/quicklang +(require "../helper.rkt") +(provide read-syntax (rename-out [#%mb #%module-begin]) ★ ★★) + +(define (read-syntax path port) + (define lines (port->lines port)) + (strip-context #`(module mod "main.rkt" + #,@(for/list ([datum (in-port read (open-input-string (car lines)))]) + datum) + #,@(map (λ (ln) (apply cons (map string->number (string-split ln "/")))) (cdr lines))))) + +(define-macro (#%mb STARS DOMS ...) + #'(#%module-begin + (time (STARS '(DOMS ...))))) + +(define (find-dominoes-with-val dominoes val) + (filter (λ (d) (or (= (car d) val) (= (cdr d) val))) dominoes)) + +(define (other-val-on-domino dom val) + ((if (= (car dom) val) cdr car) dom)) + +(define (remove-dom doms dom) + (filter-not (λ (d) (equal? d dom)) doms)) + +(define (bridges dominoes [current-val 0] [current-bridge null]) + (define doms (find-dominoes-with-val dominoes current-val)) + (if (null? doms) + (list (flatten current-bridge)) + (append-map (λ (dom) (bridges (remove-dom dominoes dom) + (other-val-on-domino dom current-val) + (cons dom current-bridge))) doms))) + +(define (strongest bridges) + (apply max (map (λ (br) (apply + br)) bridges))) + +(define (★ dominoes) + (strongest (bridges dominoes))) + +(define (★★ dominoes) + (define brs (bridges dominoes)) + (define maxlen (apply max (map length brs))) + (strongest (filter (λ (b) (= maxlen (length b))) brs))) + diff --git a/2017/d24/star1.rkt b/2017/d24/star1.rkt new file mode 100644 index 0000000..90d6b06 --- /dev/null +++ b/2017/d24/star1.rkt @@ -0,0 +1,58 @@ +#lang reader "main.rkt" ★ ; 1868 +25/13 +4/43 +42/42 +39/40 +17/18 +30/7 +12/12 +32/28 +9/28 +1/1 +16/7 +47/43 +34/16 +39/36 +6/4 +3/2 +10/49 +46/50 +18/25 +2/23 +3/21 +5/24 +46/26 +50/19 +26/41 +1/50 +47/41 +39/50 +12/14 +11/19 +28/2 +38/47 +5/5 +38/34 +39/39 +17/34 +42/16 +32/23 +13/21 +28/6 +6/20 +1/30 +44/21 +11/28 +14/17 +33/33 +17/43 +31/13 +11/21 +31/39 +0/9 +13/50 +10/14 +16/10 +3/24 +7/0 +50/50 \ No newline at end of file diff --git a/2017/d24/star2.rkt b/2017/d24/star2.rkt new file mode 100644 index 0000000..2948b39 --- /dev/null +++ b/2017/d24/star2.rkt @@ -0,0 +1,58 @@ +#lang reader "main.rkt" ★★ ; 1841 +25/13 +4/43 +42/42 +39/40 +17/18 +30/7 +12/12 +32/28 +9/28 +1/1 +16/7 +47/43 +34/16 +39/36 +6/4 +3/2 +10/49 +46/50 +18/25 +2/23 +3/21 +5/24 +46/26 +50/19 +26/41 +1/50 +47/41 +39/50 +12/14 +11/19 +28/2 +38/47 +5/5 +38/34 +39/39 +17/34 +42/16 +32/23 +13/21 +28/6 +6/20 +1/30 +44/21 +11/28 +14/17 +33/33 +17/43 +31/13 +11/21 +31/39 +0/9 +13/50 +10/14 +16/10 +3/24 +7/0 +50/50 \ No newline at end of file diff --git a/2017/d24/test1.rkt b/2017/d24/test1.rkt new file mode 100644 index 0000000..15c169f --- /dev/null +++ b/2017/d24/test1.rkt @@ -0,0 +1,9 @@ +#lang reader "main.rkt" ★ ; 31 +0/2 +2/2 +2/3 +3/4 +3/5 +0/1 +10/1 +9/10 diff --git a/2017/d24/test2.rkt b/2017/d24/test2.rkt new file mode 100644 index 0000000..a8da4d3 --- /dev/null +++ b/2017/d24/test2.rkt @@ -0,0 +1,9 @@ +#lang reader "main.rkt" ★★ ; 19 +0/2 +2/2 +2/3 +3/4 +3/5 +0/1 +10/1 +9/10