day 8: better???

This commit is contained in:
xenia 2022-12-08 00:28:57 -05:00
parent bbb31f624c
commit d7e10915f2
1 changed files with 32 additions and 33 deletions

47
8.rkt
View File

@ -4,10 +4,17 @@
;; solution for day 8 ;; solution for day 8
(define (visible? grid y x) (define (g-ref grid y x)
(define h (vector-length grid)) (vector-ref (vector-ref grid y) x))
(define w (vector-length (vector-ref grid 0)))
(define v (vector-ref (vector-ref grid y) x)) (define (g-size grid)
(values (vector-length grid) (vector-length (vector-ref grid 0))))
(define (part1 grid)
(define-values [h w] (g-size grid))
(define (visible? y x)
(define v (g-ref grid y x))
(cond (cond
[(= 0 y) #t] [(= 0 y) #t]
[(= 0 x) #t] [(= 0 x) #t]
@ -16,38 +23,30 @@
[else [else
(or (or
(for/and ([x* (in-range 0 x)]) (for/and ([x* (in-range 0 x)])
(< (vector-ref (vector-ref grid y) x*) v)) (< (g-ref grid y x*) v))
(for/and ([x* (in-range (add1 x) w)]) (for/and ([x* (in-range (add1 x) w)])
(< (vector-ref (vector-ref grid y) x*) v)) (< (g-ref grid y x*) v))
(for/and ([y* (in-range 0 y)]) (for/and ([y* (in-range 0 y)])
(< (vector-ref (vector-ref grid y*) x) v)) (< (g-ref grid y* x) v))
(for/and ([y* (in-range (add1 y) h)]) (for/and ([y* (in-range (add1 y) h)])
(< (vector-ref (vector-ref grid y*) x) v)))])) (< (g-ref grid y* x) v)))]))
(for*/sum ([x (in-range w)] [y (in-range h)] #:when (visible? y x))
(define (part1 input)
(define h (vector-length input))
(define w (vector-length (vector-ref input 0)))
(for*/sum ([x (in-range w)] [y (in-range h)]
#:when (visible? input y x))
1)) 1))
(define (part2 grid)
(define (part2 input) (define-values [h w] (g-size grid))
(define h (vector-length input))
(define w (vector-length (vector-ref input 0)))
(define (scenic-score y x) (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) 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) 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) 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))) 1)))
(apply max (apply max