ã·ãã·ã19é£éJavaScriptç
ã²ã¼ã ãã·ãã·ããã§ããã£ã¼ã«ãã®ç¶æ ãããã¹ãã§ä¸ããããã¨ããæ¶ãããã·ãããæ¶ãã¦æ¬¡ã®ãã£ã¼ã«ãã®ç¶æ ãåºåããããã°ã©ã ãæ¸ãã
http://okajima.air-nifty.com/b/2011/01/2011-ffac.html
ååæ¸ããHaskellçã®ã¢ã«ã´ãªãºã ã®ã¾ã¾JavaScriptã«ç§»æ¤ãã¦ã¿ãã
const partition = (f,xs) => xs.reduce(([y,z],x) => f(x) ? [[...y,x],z] : [y,[...z,x]],[[],[]]) const cat = (x,[ok,ng]) => [[x,...ok.flat()],...ng] const cmp = ([i,j,],[ii,jj,]) => i==ii ? j-jj : i-ii const near = ([i,j,c],[ii,jj,cc]) => c==cc && (ii-i)**2+(jj-j)**2==1 const xyc = (n,xs) => xs.map((x,i) => [i/n|0,i%n,x]) const tr = xs => xs.map(([i,j,c]) => [j,i,c]) const str = xs => xs.sort(cmp).map(([,,c])=>c) const joins = xs => xs.filter(([,,c])=>c.match(/\S/)) .reduce((r,x)=>cat(x,partition(ys=>ys.some(y=>near(x,y)),r)),[]) const puyo = obj => { const xs = tr(xyc(obj.cols,obj.value.split(""))) const j4 = joins(xs).filter(x=>x.length>3).flat() obj.value = str(tr(xyc(obj.rows,str(xs.map(x=>j4.some(y=>x+""==y+"") ? [x[0],-1," "] : x))))).join("") }
<!DOCTYPE html> <title>puyo</title> <script src=puyo.js></script> <form name=f> <input type=button value=next onclick='puyo(document.f.t)'> <input type=reset value=reset><br> <textarea name=t rows=13 cols=7> GYRR RYYGYG GYGYRR RYGYRG YGYRYG GYRYRG YGYRYR YGYRYR YRRGRG RYGYGG GRYGYR GRYGYR GRYGYR </textarea> </form>