जावास्क्रिप्ट (ईएस 6) 257 288 321
मर्ज किए गए चरणों को संपादित करें । कुछ और चार्ट को काटने के लिए गोल्फ कोड को
संपादित करें
वर्तमान एक्स और वाई स्थिति और वर्तमान दिशा का ट्रैक रखते हुए आर सरणी में पुनरावृत्त रूप से आउटपुट का निर्माण करें। जब x या y स्थिति <0 होती है, तो संपूर्ण r सरणी को पुन: अन्याय किया जाता है।
मुख्य चर:
- आर परिणाम सरणी या पंक्तियाँ
- x, y वर्तमान स्थिति।
- वर्तमान दिशा (0..7) (या वर्तमान स्थिति)
- d वर्तमान चिन्ह (0..3) -> '| \ _ /'
- वर्तमान क्रम पर l रननिग स्थिति (नीचे ०)
- डब्ल्यू वर्तमान सर्पिल त्रिज्या (कम या ज्यादा)
F=n=>
(w=>{
for(r=b=[],s=y=x=d=0;n--;
d&&--l||((s=s+1&7,d=s&3)?l=d-2?w:s/2-2+w+w:w+=!s))
s>0&s<4?++x:s>4?x?--x:r=r.map(v=>' '+v):b+=' ',
q=r[s>2&s<6?++y:y]||b,
r[y]=(q+b).slice(0,x)+'|/_\\'[d]+q.slice(x+1),
s<2|s>6?y?--y:r=[,...r]:x+=!d*2,x-=!d
})(1)||r.join('\n')
Ungolfed
F=n=>{
var r=[], s,x,y,d,w,l, q
for(l=w=1, s=x=y=d=0; n--;)
{
if (s>2 && s<6) ++y; // right side, inc y before drawing
if (x < 0) // too left, adjust
{
r = r.map(v=>' '+v) // shift all to right
++x; // move current position to right
}
if (y < 0) // too up
{
r = [q='',...r] // shift all to bottom
++y; // move current position to bottom
}
q = r[y] || ''; // current row, if undefined convert to empty string
r[y] = (q+' '.repeat(x)).slice(0,x) + '|/_\\'[d] + q.slice(x+1); // add current symbol in the x column
if (s<2 || s>6) --y; // left side, dec y after drawing
if (s>0 && s<4) // always change x after drawing
++x;
else if (s > 4)
--x;
--l; // decrement current run
if (l == 0) // if 0, need to change direction
{
s = (s+1) % 8; // change direction
d = s % 4; // change symbol
if (d == 0)
{
// vertical direction, adjust x and if at 0 increase radius
l = 1 // always 1 vertical step
if (s == 0)
++x, ++w
else
--x
}
else
{
if (d != 2)
{
l = w; // diaagonal length is always w
}
else if (s == 2)
{
l = w+w-1 // top is radius * 2 -1
}
else
{
l = w+w+1 // bottom is radius * 2 +1
}
}
}
}
return r.join('\n')
}
टेस्ट में फ़ायरफ़ॉक्स / फ़ायरबग कंसोल (या JSFiddle THX @Rainbolt)
;[1, 2, 10, 20, 155, 278].forEach(x=>console.log(F(x)))
उत्पादन
|
/
|
_
/ \
| |
\___/
___
/ _ \
/ / \ \
| | | |
\___/ /
___________
/ _________ \
/ / _______ \ \
/ / / _____ \ \ \
/ / / / ___ \ \ \ \
/ / / / / _ \ \ \ \ \
/ / / / / / \ \ \ \ \ \
| | | | | | | | | | | |
\ \ \ \ \___/ / / / /
\ \ \ \_____/ / / /
\ \ \_______/ / /
\ \_________/ /
\___________/
_______________
/ _____________ \
/ / ___________ \ \
/ / / _________ \ \ \
/ / / / _______ \ \ \ \
/ / / / / _____ \ \ \ \ \
/ / / / / / ___ \ \ \ \ \ \
/ / / / / / / _ \ \ \ \ \ \ \
/ / / / / / / / \ \ \ \ \ \ \ \
| | | | | | | | | | | | | | | |
\ \ \ \ \ \ \___/ / / / / / / /
\ \ \ \ \ \_____/ / / / / / /
\ \ \ \ \_______/ / / / / /
\ \ \ \_________/ / / / /
\ \ \___________/ / / /
\ \_____________/ / /
\_______________/ /