#lang racket (define input (string-split (string-trim (file->string "input.txt")))) (define map-height (length input)) (define map-width (length (string->list (first input)))) (define tree-map (for*/hash ([(line i) (in-indexed input)] [(char j) (in-indexed line)]) (values (cons i j) char))) (define (is-square top-left offset) (match-define (cons x y) top-left) (and (equal? #\# (hash-ref tree-map (cons x y) #\.)) (equal? #\# (hash-ref tree-map (cons (+ x offset) y) #\.)) (equal? #\# (hash-ref tree-map (cons x (+ y offset)) #\.)) (equal? #\# (hash-ref tree-map (cons (+ x offset) (+ y offset)) #\.)))) (define (count-squares-from position offset) (for*/sum ([offset (in-range 1 offset)]) (if (is-square position offset) 1 0))) (for*/sum ([i (build-list map-height values)] [j (build-list map-width values)]) (define max-offset (max (- map-height i) (- map-width j))) (count-squares-from (cons i j) max-offset))