2016
Matthew Butterick 8 years ago
parent e39fa81687
commit 1d1c9d323b

@ -17,23 +17,23 @@
(define (solve dim num) (define (solve dim num)
(define open? (make-open-pred num)) (define open? (make-open-pred num))
(define g (undirected-graph '((0 0)))) (define g (undirected-graph '((0 0))))
(for* ([x (in-range dim)] (for* ([y (in-range dim)]
[y (in-range dim)] [x (in-range dim)]
#:when (open? (list y x))) #:when (open? (cons x y)))
(when (open? (list (add1 y) x)) (when (open? (cons (add1 x) y))
(add-edge! g (list y x) (list (add1 y) x))) (add-edge! g (cons x y) (cons (add1 x) y)))
(when (open? (list y (add1 x))) (when (open? (cons x (add1 y)))
(add-edge! g (list y x) (list y (add1 x))))) (add-edge! g (cons x y) (cons x (add1 y)))))
(define path (fewest-vertices-path g '(1 1) '(31 39))) (define path (fewest-vertices-path g '(1 . 1) '(31 . 39)))
(displayln (and path (sub1 (length path))))) (displayln (and path (sub1 (length path)))))
(define (solve2 num) (define (solve2 num)
(define open? (make-open-pred num)) (define open? (make-open-pred num))
(define (nonnegative? pt) (and (not (negative? (car pt))) (define (nonnegative? pt) (and (not (negative? (car pt)))
(not (negative? (cadr pt))) (not (negative? (cdr pt)))
pt)) pt))
(let loop ([all-visited-pts empty] (let loop ([all-visited-pts empty]
[last-visited-pts '((1 1))] [last-visited-pts '((1 . 1))]
[step 0]) [step 0])
(cond (cond
[(= step 50) [(= step 50)
@ -42,11 +42,11 @@
(loop (append last-visited-pts all-visited-pts) (loop (append last-visited-pts all-visited-pts)
(append* (append*
(for/list ([lvp (in-list last-visited-pts)]) (for/list ([lvp (in-list last-visited-pts)])
(match-define (list x y) lvp) (match-define (cons x y) lvp)
(for/list ([pt (in-list (list (list (add1 x) y) (for/list ([pt (in-list (list (cons (add1 x) y)
(list x (add1 y)) (cons x (add1 y))
(list (sub1 x) y) (cons (sub1 x) y)
(list x (sub1 y))))] (cons x (sub1 y))))]
#:when (and (nonnegative? pt) #:when (and (nonnegative? pt)
(open? pt) (open? pt)
(not (member pt all-visited-pts)))) (not (member pt all-visited-pts))))
@ -55,7 +55,7 @@
(define (make-open-pred num) (define (make-open-pred num)
(λ(pt) (λ(pt)
(match-define (list x y) pt) (match-define (cons x y) pt)
(define sum (define sum
(+ (* x x) (* 3 x) (* 2 x y) y (* y y) num)) (+ (* x x) (* 3 x) (* 2 x y) y (* y y) num))
(even? (even?