aoc2022/8.rkt

66 lines
1.8 KiB
Racket

#lang curly-fn racket
(require "scripts/aoc.rkt")
;; solution for day 8
(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 grid)
(define-values [h w] (g-size grid))
(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 grid)
(define-values [h w] (g-size grid))
(define (scenic-score y x)
(define v (g-ref grid y x))
(*
(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 (>= (g-ref grid y x*) v))
1)
(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 (>= (g-ref grid y* x) v))
1)))
(apply max
(for*/list ([y (in-range h)] [x (in-range w)])
(scenic-score y x))))
(define (parse fname)
(for/vector ([line (in-list (file->lines fname))])
(for/vector ([chr (in-list (string->list (string-trim line)))])
(- (char->integer chr) (char->integer #\0)))))
(module+ main
(define input (parse "inputs/8"))
(answer 8 1 (time (part1 input)))
(answer 8 2 (time (part2 input)))
(displayln "meow"))