day 8: better???
This commit is contained in:
parent
bbb31f624c
commit
d7e10915f2
65
8.rkt
65
8.rkt
|
@ -4,50 +4,49 @@
|
|||
|
||||
;; solution for day 8
|
||||
|
||||
(define (visible? grid y x)
|
||||
(define h (vector-length grid))
|
||||
(define w (vector-length (vector-ref grid 0)))
|
||||
(define v (vector-ref (vector-ref grid y) x))
|
||||
(cond
|
||||
[(= 0 y) #t]
|
||||
[(= 0 x) #t]
|
||||
[(= (sub1 h) y) #t]
|
||||
[(= (sub1 w) x) #t]
|
||||
[else
|
||||
(or
|
||||
(for/and ([x* (in-range 0 x)])
|
||||
(< (vector-ref (vector-ref grid y) x*) v))
|
||||
(for/and ([x* (in-range (add1 x) w)])
|
||||
(< (vector-ref (vector-ref grid y) x*) v))
|
||||
(for/and ([y* (in-range 0 y)])
|
||||
(< (vector-ref (vector-ref grid y*) x) v))
|
||||
(for/and ([y* (in-range (add1 y) h)])
|
||||
(< (vector-ref (vector-ref grid y*) x) v)))]))
|
||||
(define (g-ref grid y x)
|
||||
(vector-ref (vector-ref grid y) x))
|
||||
|
||||
(define (g-size grid)
|
||||
(values (vector-length grid) (vector-length (vector-ref grid 0))))
|
||||
|
||||
(define (part1 input)
|
||||
(define h (vector-length input))
|
||||
(define w (vector-length (vector-ref input 0)))
|
||||
(define (part1 grid)
|
||||
(define-values [h w] (g-size grid))
|
||||
|
||||
(for*/sum ([x (in-range w)] [y (in-range h)]
|
||||
#:when (visible? input y x))
|
||||
(define (visible? y x)
|
||||
(define v (g-ref grid y x))
|
||||
(cond
|
||||
[(= 0 y) #t]
|
||||
[(= 0 x) #t]
|
||||
[(= (sub1 h) y) #t]
|
||||
[(= (sub1 w) x) #t]
|
||||
[else
|
||||
(or
|
||||
(for/and ([x* (in-range 0 x)])
|
||||
(< (g-ref grid y x*) v))
|
||||
(for/and ([x* (in-range (add1 x) w)])
|
||||
(< (g-ref grid y x*) v))
|
||||
(for/and ([y* (in-range 0 y)])
|
||||
(< (g-ref grid y* x) v))
|
||||
(for/and ([y* (in-range (add1 y) h)])
|
||||
(< (g-ref grid y* x) v)))]))
|
||||
|
||||
(for*/sum ([x (in-range w)] [y (in-range h)] #:when (visible? y x))
|
||||
1))
|
||||
|
||||
|
||||
(define (part2 input)
|
||||
(define h (vector-length input))
|
||||
(define w (vector-length (vector-ref input 0)))
|
||||
(define (part2 grid)
|
||||
(define-values [h w] (g-size grid))
|
||||
|
||||
(define (scenic-score y x)
|
||||
(define v (vector-ref (vector-ref input y) x))
|
||||
(define v (g-ref grid y x))
|
||||
(*
|
||||
(for/sum ([x* (in-range (sub1 x) -1 -1)] #:final (>= (vector-ref (vector-ref input y) x*) v))
|
||||
(for/sum ([x* (in-range (sub1 x) -1 -1)] #:final (>= (g-ref grid y x*) v))
|
||||
1)
|
||||
(for/sum ([x* (in-range (add1 x) w)] #:final (>= (vector-ref (vector-ref input y) x*) v))
|
||||
(for/sum ([x* (in-range (add1 x) w)] #:final (>= (g-ref grid y x*) v))
|
||||
1)
|
||||
(for/sum ([y* (in-range (sub1 y) -1 -1)] #:final (>= (vector-ref (vector-ref input y*) x) v))
|
||||
(for/sum ([y* (in-range (sub1 y) -1 -1)] #:final (>= (g-ref grid y* x) v))
|
||||
1)
|
||||
(for/sum ([y* (in-range (add1 y) h)] #:final (>= (vector-ref (vector-ref input y*) x) v))
|
||||
(for/sum ([y* (in-range (add1 y) h)] #:final (>= (g-ref grid y* x) v))
|
||||
1)))
|
||||
|
||||
(apply max
|
||||
|
|
Loading…
Reference in New Issue