#lang racket (define S '((2 9 4) (7 5 3) (6 1 8))) (define (transpose xss) (apply map list xss)) (define all-swaps (list S (reverse S) (map reverse S) (reverse (map reverse S)))) (define all-squares (append all-swaps (map transpose all-swaps))) (pretty-print all-squares (current-error-port)) (define (distance sq1 sq2) (apply + (for/list ((r1 sq1) (r2 sq2)) (apply + (for/list ((c1 r1) (c2 r2)) (abs (- c1 c2))))))) (define (solution) (define sq (for/list ((_ 3)) (for/list ((_ 3)) (read)))) (apply min (for/list ((magic all-squares)) (distance sq magic)))) (module+ main (solution))