67 lines
2.0 KiB
Racket
67 lines
2.0 KiB
Racket
|
#lang curly-fn racket
|
||
|
|
||
|
(require "scripts/aoc.rkt")
|
||
|
|
||
|
;; 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 (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))
|
||
|
|
||
|
|
||
|
(define (part2 input)
|
||
|
(define h (vector-length input))
|
||
|
(define w (vector-length (vector-ref input 0)))
|
||
|
|
||
|
(define (scenic-score y x)
|
||
|
(define v (vector-ref (vector-ref input y) x))
|
||
|
(*
|
||
|
(for/sum ([x* (in-range (sub1 x) -1 -1)] #:final (>= (vector-ref (vector-ref input y) x*) v))
|
||
|
1)
|
||
|
(for/sum ([x* (in-range (add1 x) w)] #:final (>= (vector-ref (vector-ref input y) x*) v))
|
||
|
1)
|
||
|
(for/sum ([y* (in-range (sub1 y) -1 -1)] #:final (>= (vector-ref (vector-ref input y*) x) v))
|
||
|
1)
|
||
|
(for/sum ([y* (in-range (add1 y) h)] #:final (>= (vector-ref (vector-ref input 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"))
|