From 87d7371ffa8a1555264aecc8d4a80d764f9479dc Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Thu, 8 Dec 2022 22:48:34 -0500 Subject: [PATCH] day8: Add part 2, unoptimized --- 2022/08/sol.scm | 55 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/2022/08/sol.scm b/2022/08/sol.scm index c74a77f..3209608 100755 --- a/2022/08/sol.scm +++ b/2022/08/sol.scm @@ -37,22 +37,59 @@ (max-in-dir? grid r c -1 0) (max-in-dir? grid r c 0 -1))))) +(define (num-in-dir grid r c dr dc) + (let ((tree (grid-get grid r c)) + (n (vector-length grid))) + (let loop ((r r) (c c) (count 0)) + (let ((nr (+ r dr)) (nc (+ c dc))) + (if (or (neg? nr) (neg? nc) + (>= nr n) (>= nc n)) + count ; made it off the map + (if (<= tree (grid-get grid nr nc)) + (add1 count) + (loop nr nc (add1 count)))))))) + +(define (score grid r c) + ; (print "visiting " r " " c) + (apply * + (filter (lambda (x) (> x 0)) + (list (num-in-dir grid r c 0 1) + (num-in-dir grid r c 1 0) + (num-in-dir grid r c -1 0) + (num-in-dir grid r c 0 -1))))) + (define (range n) (if (equal? n 0) '(0) (append (range (sub1 n)) (list n)))) +(define (filter pred lst) + (if (null? lst) + '() + (if (pred (car lst)) + (cons (car lst) (filter pred (cdr lst))) + (filter pred (cdr lst))))) + +(define (count-visible grid) + (let ((n (sub1 (vector-length grid)))) + (let rloop ((r 0) (count 0)) + (if (> r n) + count + (rloop (add1 r) + (+ count (length (filter (lambda (c) (is-visible? grid r c)) + (range n))))))))) + +(define (best-view grid) + (let ((n (sub1 (vector-length grid)))) + (apply max (map (lambda (r) + (apply max (map (lambda (c) (score grid r c)) (range n)))) + (range n))))) + (define (main args) (let* ((lines (read-lines)) - (grid (lines->grid lines)) - (n (sub1 (length lines)))) - (let ((visible (map - (lambda (r) - (map (lambda (c) (if (is-visible? grid r c) 1 0)) - (range n))) - (range n)))) - ; (for-each print visible) - (print (apply + (map (lambda (l) (apply + l)) visible)))))) + (grid (lines->grid lines))) + (print (count-visible grid)) + (print (best-view grid)))) ;; for repl exploration (define gd '#(#(3 0 3 7 3) -- libgit2 1.8.1