From d7e10915f237a99065ce1e1303e782a33bcd05cb Mon Sep 17 00:00:00 2001 From: xenia Date: Thu, 8 Dec 2022 00:28:57 -0500 Subject: [PATCH] day 8: better??? --- 8.rkt | 65 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/8.rkt b/8.rkt index e031f9c..4223810 100644 --- a/8.rkt +++ b/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