From fd38e2a07fc97be1d719e46a10b6774b5de53691 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 14 Dec 2024 18:11:11 +0100 Subject: Switch from ‘cond’ to ‘case’ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2024/12/puzzles.lisp | 73 +++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 38 deletions(-) (limited to '2024/12/puzzles.lisp') diff --git a/2024/12/puzzles.lisp b/2024/12/puzzles.lisp index a07575e..9584ea3 100644 --- a/2024/12/puzzles.lisp +++ b/2024/12/puzzles.lisp @@ -62,44 +62,41 @@ ;; figure out which 2 diagonal-neighbors would turn our ‘T’ shape into ;; a rectangle, and use them to determine how many corners we have. (let ((n (length neighbors))) - (cond - ((= n 0) 4) - ((= n 1) 2) - ((= n 2) - (let* ((l (first neighbors)) - (r (second neighbors)) - (li (car l)) (ri (car r)) - (lj (cdr l)) (rj (cdr r))) - (cond - ((or (= li ri i) (= lj rj j)) 0) - ((char= char (try-aref - (if (= li i) ri li) - (if (= lj j) rj lj))) - 1) - (:else 2)))) - ((= n 3) - (let ((wart (loop for n in neighbors - if (/= (car n) i) - collect n into odd-one-out-i - else - collect n into odd-one-out-j - finally (return - (first (if (= 1 (length odd-one-out-i)) - odd-one-out-i - odd-one-out-j)))))) - (loop for n in neighbors - count (not (or (equal n wart) - (char= char (try-aref - (if (= (car n) i) - (car wart) - (car n)) - (if (= (cdr n) j) - (cdr wart) - (cdr n))))))))) - ((= n 4) - (loop for (i . j) in (list (cons (1- i) (1- j)) (cons (1- i) (1+ j)) - (cons (1+ i) (1- j)) (cons (1+ i) (1+ j))) - count (char/= (try-aref i j) char)))))) + (case n + (0 4) + (1 2) + (2 (let* ((l (first neighbors)) + (r (second neighbors)) + (li (car l)) (ri (car r)) + (lj (cdr l)) (rj (cdr r))) + (cond + ((or (= li ri i) (= lj rj j)) 0) + ((char= char (try-aref + (if (= li i) ri li) + (if (= lj j) rj lj))) + 1) + (:else 2)))) + (3 (let ((wart (loop for n in neighbors + if (/= (car n) i) + collect n into odd-one-out-i + else + collect n into odd-one-out-j + finally (return + (first (if (= 1 (length odd-one-out-i)) + odd-one-out-i + odd-one-out-j)))))) + (loop for n in neighbors + count (not (or (equal n wart) + (char= char (try-aref + (if (= (car n) i) + (car wart) + (car n)) + (if (= (cdr n) j) + (cdr wart) + (cdr n))))))))) + (4 (loop for (i . j) in (list (cons (1- i) (1- j)) (cons (1- i) (1+ j)) + (cons (1+ i) (1- j)) (cons (1+ i) (1+ j))) + count (char/= (try-aref i j) char)))))) ;; END PART 2 (defun try-aref (i j) -- cgit v1.2.3