四路交叉口发电机


26

这是四向交叉的ASCII艺术:

     |  |  |
     |     |
     |  |  |
     |     |
     |  |  |
-----+-----+-----
     |     |     
- - -|     |- - -
     |     |     
-----+-----+-----
     |  |  |
     |     |
     |  |  |
     |     |
     |  |  |

(请注意,水平道路的高度为3行,而垂直道路的宽度为5列。这是出于审美原因,因为是矩形字体。)

您的挑战是产生这种ASCII艺术。但是,正如我确定的大家所知道的,并不是每个交叉路口都有在每个方向上行驶的道路。这个特定的路口走了NESW,但是某些路口可能走了,例如NW

     |  |  |
     |     |
     |  |  |
     |     |
     |  |  |
-----+-----+
     |     |
- - -|     |
     |     |
-----+-----+

否则可能会SWE

-----+-----+-----
     |     |     
- - -|     |- - -
     |     |     
-----+-----+-----
     |  |  |
     |     |
     |  |  |
     |     |
     |  |  |

甚至可能E只是一个方向而已(尽管您几乎不能称其为十字路口,但要尽量避免过于腐):

     +-----+-----
     |     |     
     |     |- - -
     |     |     
     +-----+-----

您需要编写一个可以轻松生成这些组合中任何一个的程序或函数。更具体地说,您面临的挑战是编写一个程序或函数,该程序或函数采用由组成的字符串NESW作为输入,并输出或返回这种ASCII码与交叉路口相交的ASCII艺术。这些方向可以按任意顺序出现,但输入将不包含除了任何字符NES,或W。如果愿意,可以要求输入小写字母,但是必须在答案中指定。您还可以假设所有输入将至少包含一个方向。

最后一个示例在每条线上都有前导空格,因为没有道路向西行驶。如果您没有向西行驶的道路,那么这些前导空格是可选的。这个:

+-----+-----
|     |     
|     |- - -
|     |     
+-----+-----

也将是可接受的输出。同样,如果NS不存在,则此处的空行是可选的。允许尾随换行符,并且只要输出在视觉上相同,就允许尾随空格。

您可以采用任何合理的格式进行输入和输出,例如STDIN / STDOUT,命令行参数,文件,函数自变量/返回值等。

像往常一样,这是,因此请尝试以碰巧使用的任何语言获得最短的答案!

样本IO:

NESW:

     |  |  |
     |     |
     |  |  |
     |     |
     |  |  |
-----+-----+-----
     |     |     
- - -|     |- - -
     |     |     
-----+-----+-----
     |  |  |
     |     |
     |  |  |
     |     |
     |  |  |


NS:

|  |  |
|     |
|  |  |
|     |
|  |  |
+-----+
|     |
|     |
|     |
+-----+
|  |  |
|     |
|  |  |
|     |
|  |  |

S:

+-----+
|     |
|     |
|     |
+-----+
|  |  |
|     |
|  |  |
|     |
|  |  |

EW:

-----+-----+-----
     |     |     
- - -|     |- - -
     |     |     
-----+-----+-----

SE:
+-----+-----
|     |     
|     |- - -
|     |     
+-----+-----
|  |  |
|     |
|  |  |
|     |
|  |  |

是否也允许尾随空格(E例如,如果没有)?是开头和结尾的空白行获准如果没有NS
格雷格·马丁

@GregMartin是的,允许这些。看到我的编辑。
DJMcMayhem

模糊地相关,您使我想起了我最常为流氓类
Sparr

Answers:


10

使用Javascript(ES6),190个 187 185字节

这是通过在17x15矩阵上迭代来构建每个字符此ASCII艺术字符的尝试。因此,输出始终由15行17列组成,并且道路的交叉点居中。

p=>eval("for(y=15,s='';y--;s+=`\n`)for(x=17;x--;)s+=' |-+'[+[a=y>4,b=y<10,c=x>4,d=x<12].every((c,i)=>c|p.search('SNEW'[i])+1)&&!((z=x-8||y&1|a&b)&&z*z-9)+2*!((z=y-7||x&1|c&d)&&z*z-4)]")

取消评论

for(y = 15, s = ''; y--; s += `\n`)   // iterate on y, from 14 to 0 / append line feeds
  for(x = 17; x--;)                   // iterate on x, from 16 to 0
    s += ' |-+' [                     // append next character to string
      +[ a = y > 4,                   // a = outside South area?
         b = y < 10,                  // b = outside North area?
         c = x > 4,                   // c = outside East area?
         d = x < 12 ]                 // d = outside West area?
      .every((c, i) =>                // for each area, see if either:
        c |                           //   - we're currently outside it
        p.search('SNEW' [i]) + 1      //   - or it's an allowed area (p = function param.)
      )                               // if all tests pass, add a:
      &&                              //   - vertical bar (encoded as 1) if:
      !((z = x - 8 || y & 1 | a & b)  //     - x=8, y is even and we're not in the middle
      && z * z - 9)                   //     - OR x=5 OR x=11 (<=> (x-8)*(x-8) = 9)
      + 2 *                           //   - horizontal bar (encoded as 2) if:
      !((z = y - 7 || x & 1 | c & d)  //     - y=7, x is even and we're not in the middle
      && z * z - 4)                   //     - OR y=5 OR y=9 (<=> (y-7)*(y-7) = 4)
    ]                                 // (vertical + horizontal = 3, which leads to '+')

矩阵

下面是代码中使用的坐标矩阵。

矩阵

演示版

以下代码段允许尝试任何道路配置。

let f =
p=>eval("for(y=15,s='';y--;s+=`\n`)for(x=17;x--;)s+=' |-+'[+[a=y>4,b=y<10,c=x>4,d=x<12].every((c,i)=>c|p.search('SNEW'[i])+1)&&!((z=x-8||y&1|a&b)&&z*z-9)+2*!((z=y-7||x&1|c&d)&&z*z-4)]")

function update() {
  document.getElementById("o").innerHTML = f(document.getElementById("s").value);
}
update();
<input id="s" size=4 value="NSEW" oninput="update()">
<pre id="o" style="font-size:9px"></pre>


8

PowerShell的V3 +,226个 204 192 191字节

param([char[]]$a)($n=0..4|%{($x=' '*5*($b=87-in$a))+('|  |  |',($w='|     |'))[$_%2]})*(78-in$a)
($z=($y='-'*5)*$b+"+$y+"+$y*($c=69-in$a))
$x+$w
'- - -'*$b+$w+'- - -'*$c
$x+$w
$z
$n*(83-in$a)

将输入作为大写字母字符串,将其显式转换为char数组。通过循环从0到构造“北部”线段4。每个循环构造一个由5个空格组成的字符串(如果输入中存在W/ 87$x,然后将其| |存储在中,然后将其存储在中(存储在中$w)或| | |,具体取决于我们当前是偶数还是奇数。该字符串数组存储在中$n,并乘以N/ 78是否-in为输入。这将确定是否$n放置在管道上。

然后,我们构造中间部分。第一行$z是东西向路线的“顶部”,使用与WE/ 相同的逻辑69,并用括号括起来以在管道上放置一个副本。我们使用辅助变量$y-----部分中保存一个字节。

下一行是$x由正确宽度的管道(即)字符串连接的适当数量的空格(即$w)。然后,中间的条纹线再次以WE逻辑$w填充在中间。然后,$x+$w$z一次。

最后,由于南路与北路相同,$n如果S/ 83是,则放置在管线上-in $a

所有这些结果字符串都是从管道中收集的,在程序执行结束时,输出是隐式的。滥用默认Write-Output定界符在元素之间插入换行符。


例子

PS C:\Tools\Scripts\golfing> .\4-way-intersection.ps1 'EN'
|  |  |
|     |
|  |  |
|     |
|  |  |
+-----+-----
|     |
|     |- - -
|     |
+-----+-----

PS C:\Tools\Scripts\golfing> .\4-way-intersection.ps1 'SNW'
     |  |  |
     |     |
     |  |  |
     |     |
     |  |  |
-----+-----+
     |     |
- - -|     |
     |     |
-----+-----+
     |  |  |
     |     |
     |  |  |
     |     |
     |  |  |

PS C:\Tools\Scripts\golfing> .\4-way-intersection.ps1 'WE'
-----+-----+-----
     |     |
- - -|     |- - -
     |     |
-----+-----+-----

4

C ++ 317个 280 276字节

int i(char*j){for(int y=0;y<15;++y)for(int x=0;x<18;++x)putchar(x-17?y<5&!strchr(j,'N')|y>9&!strchr(j,'S')|x<5&!strchr(j,'W')|x>11&!strchr(j,'E')?' ' :x==5|x==11?y==5|y==9?'+':'|':y==5|y==9?x==5|x==11?'+':'-':x==8&y%2|y==7&x%2?' ':x==8&(y/5-1)?'|':y==7&(x/6-1)?'-':' ':'\n');}

取消高尔夫:

int i (char* j)
{
  for (int y=0; y<15; ++y)
    for (int x=0; x<18; ++x)
      putchar(
        x-17 ? 
        y<5 & !strchr(j,'N') |
        y>9 & !strchr(j,'S') |
        x<5 & !strchr(j,'W') |
        x>11 & !strchr(j,'E') ? ' ' :
        x==5 | x==11 ? y==5 | y==9 ? '+' : '|' : 
        y==5 | y==9 ? x==5 | x==11 ? '+' : '-' : 
        x==8 & y%2 | y==7 & x%2 ? ' ' : 
        x==8 & (y / 5 - 1) ? '|' :
        y==7 & (x / 6 - 1) ? '-' :
        ' ' : '\n');
}

1
神圣的嵌套三元运算符,蝙蝠侠!

我们一直都知道他们会有所帮助。
David Schwartz

替换strchrindex将减少更多。定义xy一起外for循环。
Wojciech Migda'3

2

Python 3,186个字节

S=' -- -  -- -  --'
lambda d:'\n'.join(S[r//4:15*('W'in d):3]+'||+  -  -| -  -  -||+'[r%4::3]+S[r//4:15*('E'in d):3]for r in[0,1,0,1,0,6,1,9,1,6,0,1,0,1,0][5-5*('N'in d):10+5*('S'in d)])

带有指令字符串的匿名lambda,例如“ NWS”

遵循的说明


2

234号

s,$,@+-----+@|     |@!     !@|     |@+-----+@,
:l;tl
/N/s,^,#,;t
:r
/S/s,@$,#,;t
/E/{s,!@,|- - -@,;s,+@,+-----@,g}
/W/{s,@!,@- - -|,;s,@+,@-----+,g;s,@|,@     |,g}
y,@!NSEW,\n|    ,
q
:
s,#,@|  |  |@|     |@|  |  |@|     |@|  |  |,
tr

如果适当的字符在线上,它将仅构建不同的部分。
最后使用@代替\n\n退回。
北部和南部是相同的,因此我基本上使用一个函数来插入它们。


2

分批,351个 344 341字节

@echo off
set/pi=
set t=     
if not "%i:n=%"=="%i%" call:l
set l=-----
set r=%l%
if "%i:w=%"=="%i%" set l=%t%
if "%i:e=%"=="%i%" set r= 
for %%s in (%l%+-----+%r% "%t%|%t%|" "%l:--=- %|%t%|%r:--=- %" "%t%|%t%|" %l%+-----+%r%) do echo %%~s
if "%i:s=%"=="%i%" exit/b
:l
call :m
call :n
:n
echo %t%^|%t%^|
:m
echo %t%^|  ^|  ^|

注意:该行set t=以五个空格结尾,并且该行if "%i:e=%"=="%i%" set r=以一个空格结尾。从STDIN接受不区分大小写的输入。编辑:通过消除d变量保存7个字节。通过for循环打印中间部分节省了3个字节。如果允许我使用单独的命令行参数,则为326 319 316字节:

@echo off
set t=%*
set/an=s=e=w=5,%t: ==%=0
set t=     
call:%n%
set r=-----%t%
call set l=%%r:~%w%,5%%
call set r=%%r:~%e%%%
for %%s in (%l%+-----+%r% "%t%|%t%|" "%l:--=- %|%t%|%r:--=- %" "%t%|%t%|" %l%+-----+%r%) do echo %%~s
goto %s%
:0
call :1
call :2
:2
echo %t%^|%t%^|
:1
echo %t%^|  ^|  ^|
:5

1

Python 2,290字节

t,s,r,i=[],[],range(5),raw_input()
for n in r:t+=[" "*5*("W"in i)+"|  "+("|"," ")[n%2]+"  |"]
exec"s+=['-'*5];s[:1]+=' '*5,;"*2;s[:2]+="- - -",
if"N"in i:print'\n'.join(t)
print'\n'.join([s[n]*("W"in i)+("|     |","+-----+")[`n`in"04"]+s[n]*("E"in i)for n in r])
if"S"in i:print'\n'.join(t)

m,t,s=[],[],[]可能是m=t=s=[]
Yttsi'9

range(5)可以保存到变量中并使用两次,而不是键入range(5)两次。
Yttsi'9

m为了什么?
奥利弗·尼

@TuukkaX,由于某种原因将t=s=[]所有内容弄乱了
Daniel

1
我现在可以确定m=t=s=[],它们都指向同一参考。
Yytsi'9

1

GolfScript,154个字节

{a?1+}:x;'-'5*:e;' '5*:b;"+"e+"+"+:f;{b'|'b'|'b n}:k;{'W'x{e}{b}if\'E'x{e}{}if n}:y;{x{b"|  |  |
"+:c k c k c}{}if}:z;1/:a;'N'z f y k'|'b+'|'+ y k f y'S'z

在线尝试!


令人印象深刻,不知道这怎么没有票。
魔术章鱼缸

1

Pyth(385个 380 373个 353字节)

打高尔夫球:

K"     |  |  |\n     |     |\n"?}\Nz++KKPKk?}\Wz?}\Ez+++*5\-*2+\+*5\-"\n     |     |\n- - -|     |- - -\n     |     |\n"+*5\-*2+\+*5\-++*2+*5\-\+"\n     |     |\n- - -|     |\n     |     |\n"*2+*5\-\+?}\Ez+"     "+*2+\+*5\-"\n     |     |\n     |     |- - -\n     |     |\n     +-----+-----"++"     +-----+\n"*3"     |     |\n""     +-----+"?}\Sz++KKPKk

取消高尔夫:

K"     |  |  |\n     |     |\n"  //sets K to first two lines of north
?}\Nz                            //if north in the input 
  ++KKPK                         //then: return K + K + K[:-1]
  k                              //else: return ""
?}\Wz                            //if West in input
  ?}\Ez                          //if East in input
    +++*5\-*2+\+*5\-"\n     |     |\n- - -|     |- - -\n     |     |\n"+*5\-*2+\+*5\-    //then: Return E+W string
    ++*2+*5\-\+"\n     |     |\n- - -|     |\n     |     |\n"*2+*5\-\+         //else: Return W string
  ?}\Ez                          //else: if east in input (and not W)
    +"     "+*2+\+*5\-"\n     |     |\n     |     |- - -\n     |     |\n     +-----+-----" //return East without West String
    ++"     +-----+\n"*3"     |     |\n""     +-----+" \\Return empty left and right intersection
?}\Sz                            //if south in input
  ++KKPK                         //return north string
  k                              //return ""

当然,如果有任何改善,请告诉我。

感谢Maltysen,节省了5个字节

你可以在这里尝试


您可以使用K代替,N然后在首次分配时,不必使用=,可以节省一个字节
Maltysen

同样N[:-1]P
Maltysen

0

Groovy(274字节)

不打高尔夫球

r{
    l->
    t='+-----+'
    s='     ';
    m='|     |'
    x='-----'
    v=(1..5).collect{s}
    nsR=(1..5).collect{[s,((it%2)?'|  |  |':m),s]}
    ewR=[x,s,'- - -',s,x]
    c=[l[3]?ewR:v,[t,m,m,m,t],l[1]?ewR:v]
    (l[0]?nsR.collect{it}:[])+((0..4).collect{x->((0..2).collect{y->c[y][x]})}​)​+​(l[2]?nsR.collect{it}:[])
}

打高尔夫球

def i(l) {t='+-----+';s='     ';m='|     |';x='-----';v=(1..5).collect{s};n=(1..5).collect{[s,((it%2)?'|  |  |':m),s]};e=[x,s,'- - -',s,x];c=[l[3]?e:v,[t,m,m,m,t],l[1]?e:v];(l[0]?n.collect{it}:[])+((0..4).collect{x->((0..2).collect{y->c[y][x]})}​)​+​(l[2]?n.collect{it}:[])}

试试看:https : //groovyconsole.appspot.com/script/5082600544665600

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.