#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 trees (filter (lambda (pos) (equal? #\# (hash-ref tree-map pos))) (hash-keys tree-map))) (define (is-square starting-two offset) (match-define (cons x1 y1) (first starting-two)) (match-define (cons x2 y2) (second starting-two)) (match-define (cons x_off y_off) offset) (and (equal? #\# (hash-ref tree-map (cons x1 y1) #\.)) (equal? #\# (hash-ref tree-map (cons x2 y2) #\.)) (equal? #\# (hash-ref tree-map (cons (+ x1 x_off) (+ y1 y_off)) #\.)) (equal? #\# (hash-ref tree-map (cons (+ x2 x_off) (+ y2 y_off)) #\.)))) (define (get-offsets tree-pair) (match-define (cons x1 y1) (first tree-pair)) (match-define (cons x2 y2) (last tree-pair)) (define diff_x (- x2 x1)) (define diff_y (- y2 y1)) (cons (cons diff_y (- diff_x)) (cons (- diff_y) diff_x))) (/ (for/sum ([pair (in-combinations trees 2)]) (match-define (cons first-side-offset second-side-offset) (get-offsets pair)) (+ (if (is-square pair first-side-offset) 1 0) (if (is-square pair second-side-offset) 1 0))) 4)