ブッダブロのような何か

言語:mathematica

random[] := 4*RandomComplex[] - 2 - 2 I

f[z_, c_] := z^2 + c

onestep[c_, limit_] := 
 NestWhileList[f[#, c] &, 0, Abs[#] < 2 &, 1, limit]

c2pair[z_] := {Re[z], Im[z]}

step[limit_, max_] := onestep[#, limit] & /@ Table[random[], {max}]

makelist[list_] := c2pair /@ Flatten[list]

budda[limit_, max_] := ListPlot[makelist[step[limit, max]]]


f:id:mikuwaorenoyome:20170329104843p:plain

オセロ必勝法 Part2

石を返す前に、石を置くのを忘れてました。
あと、initableをちょっと修正しました。

(defun Osero ()
  (let ((table nil))
  (defun initable (table sq)
    (defun initableat (table)
      (cond ((not table) (setq table '((0 0))))
	    ((= (cadr (car table)) (1- sq)) (setq table (push `(,(1+ (caar table)) 0) table)))
	    (t (setq table (push `(,(caar table) ,(1+ (cadr (car table)))) table)))
	    table))
    (defun nest (f x n)
      (let ((i 0)
	    (out x))
	(while (<= i n)
	  (setq out (funcall f out))
	  (setq i (1+ i)))
	out))
    (defun nulltable (table)
      (mapcar '(lambda (x) (list x 'null)) table))
    (setq table (nulltable (reverse (nest 'initableat nil (1- (* sq sq))))))))
  (defun setdisk (table sq x y disk)
    (setf (cadr (elt table (+ (* sq x) y))) disk)
    table))

(Osero)
setdisk
(initable nil 2)
(((0 0) null) ((0 1) null) ((1 0) null) ((1 1) null))
(setdisk (initable nil 2) 2 1 0 'white)
(((0 0) null) ((0 1) null) ((1 0) white) ((1 1) null))

今度こそ、石を返そうと思います。

オセロ必勝法 Part1

8×8のオセロの必勝法が知りたいので、それを目標にプログラミングしていきたいと思います。
手始めに、オセロ盤のマスを作りました。
言語はEmacs Lispです。

(defun Osero ()
  (let ((table nil))
  (defun initable (table sq)
    (defun initableat (table)
      (cond ((not table) (setq table '((0 0))))
	    ((= (cadr (car table)) (1- sq)) (setq table (push `(,(1+ (caar table)) 0) table)))
	    (t (setq table (push `(,(caar table) ,(1+ (cadr (car table)))) table)))
	    table))
    (defun nest (f x n)
      (let ((i 0)
	    (out x))
	(while (<= i n)
	  (setq out (funcall f out))
	  (setq i (1+ i)))
	out))
    (setq table (nest 'initableat nil (1- (* sq sq)))))))

(Osero)
initable
(initable nil 2)
((1 1) (1 0) (0 1) (0 0))

次回は、石を返すプログラムを作ろうと思います。

セルオートマトンのルール30の音

言語:mathematica

ameba[max_] := Prepend[Table[0, {i, max - 1}], 1]

rule30[x_, y_, z_] := 
 If[x == 1, (1 - y) (1 - z), (1 - (1 - y) (1 - z))]

evolveat[list_, n_, max_] := (cell = list[[n]];
  If[n == 1, leftcell = list[[max]], leftcell = list[[n - 1]]];
  If[n == max, rightcell = list[[1]], rightcell = list[[n + 1]]];
  rule30[leftcell, cell, rightcell])

evolve[list_, max_] := (Table[evolveat[list, i, max], {i, 1, max}])

main[max_] := (imogai = NestList[evolve[#, max] &, ameba[max], max];
  ArrayPlot[imogai, PixelConstrained -> 1, Frame -> False] ListPlay[
    Flatten[imogai]])

f:id:mikuwaorenoyome:20170325190214p:plain

曲は直接貼れないので、下で聞いてください
Rule30 by いえねこ | Free Listening on SoundCloud

チューリングマシン

言語:Emacs Lisp

(defun turing (input)
  (let ((tape (make-list 16 0))
	(head '(0 0))
	(prog nil))
    
    (defun readhead ()
      (elt tape (car head)))
    (defun writehead (x)
      (setf (elt tape (car head)) x))
    (defun movehead (x)
      (if (equal x 'R)
	  (setf (car head) (1+ (car head)))
	(setf (car head) (1- (car head)))))
    (defun memoryhead (x)
      (setf (cadr head) x))
    (defun printf ()
      tape)
    (defun main ()
      (setq prog input)
      (setq head '(0 0))
      (while (not (equal (cadr head) nil))
	  (dolist (x prog)
	    (if (equal (cadr head) (car x))
		(if (equal (readhead) (elt x 1))
		    (progn (writehead (elt x 3))
			   (memoryhead (elt x 4))
			   (movehead (elt x 2))))))))
    (main)
    (printf)))

謎のバグがあって、一回目の評価だけしか正しくないです。
headの初期化と関係してるっぽいけど、よくわからない・・・

(turing '((0 0 R 1 1) (1 0 R 2 nil)))
(1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

バーニングシップ・フラクタル

言語:Mathematica

burnp[c_, limit_, f_] := (
  n = 0;
  z = 0;
  While[n < limit,
   z = f[z, c];
   If[Abs[z] >= 2, out = False; Break[]]
    If[n == limit - 1, out = True; Break[]]
    n++;];
  out)
list[table_] := (
  Map[{Re[#], Im[#]} &, table])
clist[sample_] := 
 Flatten[Table[-2 + -2 I + i/sample*4 + j/sample*4 I, {i, 0, 
    sample}, {j, 0, sample}]]
main[sample_, limit_, f_] :=
 ListPlot[list[Select[clist[sample], burnp[#, limit, f] &]]]

実行例
f:id:mikuwaorenoyome:20170121174325p:plain