Add for/max and for*/max stolen from Racket docs to library
This commit is contained in:
parent
cfb363930a
commit
06542efb47
31
lib.rkt
31
lib.rkt
|
@ -7,12 +7,41 @@
|
||||||
enqueue!)
|
enqueue!)
|
||||||
(only-in racket/set
|
(only-in racket/set
|
||||||
list->set
|
list->set
|
||||||
set->list))
|
set->list)
|
||||||
|
(for-syntax syntax/for-body)
|
||||||
|
syntax/parse/define)
|
||||||
|
|
||||||
(provide (all-from-out threading)
|
(provide (all-from-out threading)
|
||||||
(all-defined-out))
|
(all-defined-out))
|
||||||
|
|
||||||
|
|
||||||
|
;; Macros ;;
|
||||||
|
|
||||||
|
(define-syntax-parse-rule (for/max clauses body ... tail-expr)
|
||||||
|
#:with original this-syntax
|
||||||
|
#:with ((pre-body ...) (post-body ...)) (split-for-body this-syntax #'(body ... tail-expr))
|
||||||
|
(for/fold/derived original
|
||||||
|
([current-max -inf.0])
|
||||||
|
clauses
|
||||||
|
pre-body ...
|
||||||
|
(define maybe-new-max (begin post-body ...))
|
||||||
|
(if (> maybe-new-max current-max)
|
||||||
|
maybe-new-max
|
||||||
|
current-max)))
|
||||||
|
|
||||||
|
(define-syntax-parse-rule (for*/max clauses body ... tail-expr)
|
||||||
|
#:with original this-syntax
|
||||||
|
#:with ((pre-body ...) (post-body ...)) (split-for-body this-syntax #'(body ... tail-expr))
|
||||||
|
(for*/fold/derived original
|
||||||
|
([current-max -inf.0])
|
||||||
|
clauses
|
||||||
|
pre-body ...
|
||||||
|
(define maybe-new-max (begin post-body ...))
|
||||||
|
(if (> maybe-new-max current-max)
|
||||||
|
maybe-new-max
|
||||||
|
current-max)))
|
||||||
|
|
||||||
|
|
||||||
;; Function helpers ;;
|
;; Function helpers ;;
|
||||||
|
|
||||||
(define ∘ compose)
|
(define ∘ compose)
|
||||||
|
|
|
@ -62,9 +62,8 @@
|
||||||
(- r r*))))
|
(- r r*))))
|
||||||
|
|
||||||
(define part2
|
(define part2
|
||||||
(maximum
|
(for*/max ([r (in-range 1 (sub1 height))]
|
||||||
(for*/list ([r (in-range 1 (sub1 height))]
|
|
||||||
[c (in-range 1 (sub1 width))])
|
[c (in-range 1 (sub1 width))])
|
||||||
(score r c))))
|
(score r c)))
|
||||||
|
|
||||||
(show-solution (part1) part2)
|
(show-solution (part1) part2)
|
Loading…
Reference in New Issue