Skip to content

Instantly share code, notes, and snippets.

@Kuniwak
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kuniwak/9237113 to your computer and use it in GitHub Desktop.
Save Kuniwak/9237113 to your computer and use it in GitHub Desktop.
「コードゴルフ:最短コードを書く!」の回答。

コードの経過

前回の提出コード

  • 142文字(初挑戦)

    r="",w=80,h=40,z=30;for(y=0;y<h;y++){for(x=0;x<w;x++){a=(w/2-x);b=(h/2-y)*2;d=Math.sqrt(a*a+b*b);if(d<z){r+="*"}else{r+="-"}}r+="\n"}return r;
  • 112文字

    r="",w=80,h=40,z=900;for(y=0;y<h;y++){for(x=0;x<w;x++){a=w/2-x;b=h-y*2;a*a+b*b<z?r+="*":r+="-"}r+="\n"}return r;
  • 87文字

    _=40,r="";for(y=-_;y<_;y+=2){for(x=-_;x<_;x++){r+=x*x+y*y<900?"*":"-"}r+="\n"}return r;
  • 86文字

    _=40,r="";for(y=-_;y<_;y+=2){for(x=-_;x<_;x++)r+=x*x+y*y<900?"*":"-";r+="\n"}return r;
  • 85文字

    _=40,r="";for(y=-_;y<_;y+=2){for(x=-_;x<_;x++)r+=x*x+y*y<900?"*":"-";r+="\n"}return r
  • 79文字

    for(y=_=40,r="";y>-_;x<_||(x=-_,r+="\n",y-=2))r+=x*x+++y*y<900?"*":"-";return r

再トライアル

  • 98文字

    for(y=0,s='';y<40;)for(x=0;x<80||!(s+='\n',++y);)s+="-*"[0|(X=x++-40)*X+(Y=2*y-40)*Y<900];return s
  • 88文字

    for(y=-40,s='';y<40;)for(x=-40;x<40||(s+='\n',y+=2,0);)s+="-*"[0|x*x+++y*y<900];return s
  • 87文字

    for(y=F=40,s='';y>-F;)for(x=-F;x<F||(s+='\n',y-=2,0);)s+="-*"[0|x*x+++y*y<900];return s
  • 80文字

    for(y=x=40,s='';x>-40||(y-=2)>-(s+='\n',x=40);)s+="-*"[0|x*x--+y*y<900];return s
  • 78文字

    for(y=x=40,s='';x+40||(y-=2)+(s+='\n',x=40);)s+="-*"[0|x*x--+y*y<900];return s

チェックプログラム

function correctCode() {
    var res = "";
    var w = 80;
    var h = 40;
    var sz = 30;

    for (var y = 0; y < h; y ++) {
        for (var x = 0; x < w; x ++) {
            var dstnc = Math.sqrt(
                  Math.pow(w / 2 - x, 2)
                + Math.pow((h / 2 - y) * 2, 2)
            );
            if (dstnc < sz) {
                res += "*";
            } else {
                res += "-";
            }
        }
        res += "\n"
    }
    return res;
}

function yourCode() {
/*
for(y=0,s='';y<40;)for(x=0;x<80||!(s+='\n',++y);)s+="-*"[0|(X=x++-40)*X+(Y=2*y-40)*Y<900];return s
for(y=-40,s='';y<40;)for(x=-40;x<40||(s+='\n',y+=2,0);)s+="-*"[0|x*x+++y*y<900];return s
for(y=F=40,s='';y>-F;)for(x=-F;x<F||(s+='\n',y-=2,0);)s+="-*"[0|x*x+++y*y<900];return s
for(y=x=40,s='';x>-40||(y-=2)>-(s+='\n',x=40);)s+="-*"[0|x*x--+y*y<900];return s
for(y=x=40,s='';x+40||(y-=2)+(s+='\n',x=40);)s+="-*"[0|x*x--+y*y<900];return s
for(y=x=40,s='';y+40;)s+='-*\n'[x+40?0|x*x--+y*y<900:(x=40,y-=2,2)];return s
for(y=x=n=40,s='';y+n;)s+='-*\n'[x+n?0|x*x--+y*y<900:(x=n,y-=2,2)];return s
*/
for(y=x=n=40,s='';y+n;)s+='-*\n'[x+n?0|x*x--+y*y<900:(x=n,y-=2,2)];return s
}


console.log(yourCode() == correctCode() ? 'Good job.' : 'Wrong.');

var actual = yourCode().split('\n');
var expected = correctCode().split('\n');

for (var i = 0, imax = Math.max(actual.length, expected.length); i < imax; i++) {
  var actualLine = actual[i] || 'Nothing.';
  var expectedLine = expected[i] || '';
  var cursorLine = '';
  for (var j = 0, jmax = Math.max(actualLine.length, expectedLine.length); j < jmax; j++) {
    cursorLine += actualLine[j] === expectedLine[j] ? ' ' : '^';
  }
  console.log(actualLine);
  console.log(cursorLine);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment