Lazy solution

This commit is contained in:
scms 2024-03-06 17:25:53 -08:00
parent d26d9d8ccc
commit c611716050
1 changed files with 19 additions and 0 deletions

19
p85.lisp Normal file
View File

@ -0,0 +1,19 @@
(defun count-rectangles (max-width max-height)
(loop with count = 0
for width from 1 upto max-width
do (loop for height from 1 upto max-height
do (incf count (* (1+ (- max-width width))
(1+ (- max-height height)))))
finally (return count)))
(defun counting-rectangles (&optional target)
;; TODO: Could terminate once height 1 is over target, right?
(loop with best-difference = 10000000
for width upfrom 1
do (loop for height from 1 upto width
for count = (count-rectangles width height)
for difference = (abs (- target count))
do (when (< difference best-difference)
(setf best-difference difference)
(format t "(~a) ~ax~a (~a)~%" difference width height (* width height))))))