RosettaCodeData/Task/One-dimensional-cellular-automata/Common-Lisp/one-dimensional-cellular-automata-1.lisp
Ingy döt Net b83f433714 tasks a-s
2013-04-10 23:57:08 -07:00

28 lines
817 B
Common Lisp

(defun value (x)
(assert (> (length x) 1))
(coerce x 'simple-bit-vector))
(defun count-neighbors-and-self (value i)
(flet ((ref (i)
(if (array-in-bounds-p value i)
(bit value i)
0)))
(declare (inline ref))
(+ (ref (1- i))
(ref i)
(ref (1+ i)))))
(defun next-cycle (value)
(let ((new-value (make-array (length value) :element-type 'bit)))
(loop for i below (length value)
do (setf (bit new-value i)
(if (= 2 (count-neighbors-and-self value i))
1
0)))
new-value))
(defun print-world (value &optional (stream *standard-output*))
(loop for i below (length value)
do (princ (if (zerop (bit value i)) #\. #\#)
stream))
(terpri stream))