aoc2022/8.rkt

66 lines
1.8 KiB
Racket
Raw Normal View History

2022-12-08 05:23:58 +00:00
#lang curly-fn racket
(require "scripts/aoc.rkt")
;; solution for day 8
2022-12-08 05:28:57 +00:00
(define (g-ref grid y x)
(vector-ref (vector-ref grid y) x))
2022-12-08 05:23:58 +00:00
2022-12-08 05:28:57 +00:00
(define (g-size grid)
(values (vector-length grid) (vector-length (vector-ref grid 0))))
2022-12-08 05:23:58 +00:00
2022-12-08 05:28:57 +00:00
(define (part1 grid)
(define-values [h w] (g-size grid))
2022-12-08 05:23:58 +00:00
2022-12-08 05:28:57 +00:00
(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)))]))
2022-12-08 05:23:58 +00:00
2022-12-08 05:28:57 +00:00
(for*/sum ([x (in-range w)] [y (in-range h)] #:when (visible? y x))
1))
2022-12-08 05:23:58 +00:00
2022-12-08 05:28:57 +00:00
(define (part2 grid)
(define-values [h w] (g-size grid))
2022-12-08 05:23:58 +00:00
(define (scenic-score y x)
2022-12-08 05:28:57 +00:00
(define v (g-ref grid y x))
2022-12-08 05:23:58 +00:00
(*
2022-12-08 05:28:57 +00:00
(for/sum ([x* (in-range (sub1 x) -1 -1)] #:final (>= (g-ref grid y x*) v))
2022-12-08 05:23:58 +00:00
1)
2022-12-08 05:28:57 +00:00
(for/sum ([x* (in-range (add1 x) w)] #:final (>= (g-ref grid y x*) v))
2022-12-08 05:23:58 +00:00
1)
2022-12-08 05:28:57 +00:00
(for/sum ([y* (in-range (sub1 y) -1 -1)] #:final (>= (g-ref grid y* x) v))
2022-12-08 05:23:58 +00:00
1)
2022-12-08 05:28:57 +00:00
(for/sum ([y* (in-range (add1 y) h)] #:final (>= (g-ref grid y* x) v))
2022-12-08 05:23:58 +00:00
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"))