走这条路


18

挑战在于遵循(绘制)路径:

  • ^n -上n行
  • vn -下降n行
  • >n -按n个字符位置
  • <n -左n个字符位置

  • n是一个大于零的整数(即,您不会收到像这样的命令>-2)。
  • 命令之间没有分隔符,格式正确的输入如下所示:>5v8<10^3,不允许其他形式的输入。
  • 命令的数量是无限的。
  • 不应再有其他字符潜入输入中。

例子。

  1. 输入是一个空字符串,输出:

    *
    
  2. 输入是>3<3:请注意,这与输出没有任何区别:

    ****
    
  3. ^3和类似v3

    *
    *
    *
    *
    
  4. 输入:>1v2,输出:

    **
     *
     *
    
  5. 输入:^4>3v2<1,输出:

    ****
    *  *
    * **
    *
    *
    
  6. 如果返回并使用相同的路径,请不要绘制任何新内容。例如>5<5

    ******
    
  7. ...虽然您没有画任何新东西,但显然可以改变位置。因此,如果您的输入如下所示:>4<2v3,则输出为:

    *****
      *
      *
      *
    
  8. 这是一个更复杂的示例:1)路径可以穿过自身2)注意,最后一条命令的最后三个步骤将整个路径向右移动。输入:v6>4^3<7,输出:

       *
       *
       *
    ********
       *   *
       *   *
       *****
    
  9. 输入

    ^2v2>3<3v3>4^5v5>3^5>4v2<4v3>4^3v3>3^5>4v2<4v3>7^5>4v2<4v3>9^3<2^2v2>4^2v2<2v3>8^5>2v4>2^4v5<3>6^5>5<5v2>5<5v2>5<4v1>8^3<1^2v2>1v2>2^3v3>2^2>1^2v2<1v3<3>11^3<2^2v2>4^2v2<2v3>5^5>5<5v2>5<5v2>5<4v1>7^5>4v2<4v3>4^3v3>3^5>4v2<3v1<1v2>3^1>1v1
    

    输出:

    *   *  *****  *****  *****  *   *     *** *  ******  *     *    *   *  ******  *****  *****
    *   *  *   *  *   *  *   *  *   *     * * *  *       *  *  *    *   *  *       *   *  *   *
    *****  *****  *****  *****  *****     * * *  ******  ** * **    *****  ******  *****  *****
    *   *  *   *  *      *        *       * * *  *        * * *       *    *       *   *  **
    *   *  *   *  *      *        *       * ***  ******   *****       *    ******  *   *  *  **
    *******************************************************************************************
    

6
当您今天发布此挑战时,我认为这将是一个适当的测试案例:^2v2>3<3v3>4^5v5>3^5>4v2<4v3>4^3v3>3^5>4v2<4v3>7^5>4v2<4v3>9^3<2^2v2>4^2v2<2v3>8^5>2v4>2^4v5<3>6^5>5<5v2>5<5v2>5<4v1>8^3<1^2v2>1v2>2^3v3>2^2>1^2v2<1v3<3>11^3<2^2v2>4^2v2<2v3>5^5>5<5v2>5<5v2>5<4v1>7^5>4v2<4v3>4^3v3>3^5>4v2<3v1<1v2>3^1>1v1
插入用户名

您能否提供参考实现?
LegionMammal978

那不是骗子吗?请决定:D
nicael

@nicael:我会说不是;I / O是不同的,似乎路径不能在另一个路径中交叉。我认为这在允许不同高尔夫策略的方式上更简单。但是,我不知道自己突然能够自己重新打开所有这些内容,我以为我只是在投票。
marinus

@marinus好吧。因此,相关:Yarr!藏宝图!
nicael

Answers:


2

MATL,71字节

1thj'.\d+'XX"@Z)XK6L)U:"K1)XK118=K94=-K62=K60=-hv]]YstY)X<1--lqg10*32+c

使用语言/编译器的当前版本(6.0.0)。可在Matlab和Octave中使用。

编辑(2016年6月21日):由于语言的更改,该代码需要进行一些修改才能在当前版本(16.0.0)中运行。您可以在线尝试包括所需的修改。

例子

>> matl
 > 1thj'.\d+'XX"@Z)XK6L)U:"K1)XK118=K94=-K62=K60=-hv]]YstY)X<1--lqg10*32+c
 > 
> ^4>3v2<1
    ****
    *  *
    * **
    *   
    *  

>> matl
 > 1thj'.\d+'XX"@Z)XK6L)U:"K1)XK118=K94=-K62=K60=-hv]]YstY)X<1--lqg10*32+c
 > 
> ^2v2>3<3v3>4^5v5>3^5>4v2<4v3>4^3v3>3^5>4v2<4v3>7^5>4v2<4v3>9^3<2^2v2>4^2v2<2v3>8^5>2v4>2^4v5<3>6^5>5<5v2>5<5v2>5<4v1>8^3<1^2v2>1v2>2^3v3>2^2>1^2v2<1v3<3>11^3<2^2v2>4^2v2<2v3>5^5>5<5v2>5<5v2>5<4v1>7^5>4v2<4v3>4^3v3>3^5>4v2<3v1<1v2>3^1>1v1
  *   *  *****  *****  *****  *   *     *** *  ******  *     *    *   *  ******  *****  *****
  *   *  *   *  *   *  *   *  *   *     * * *  *       *  *  *    *   *  *       *   *  *   *
  *****  *****  *****  *****  *****     * * *  ******  ** * **    *****  ******  *****  *****
  *   *  *   *  *      *        *       * * *  *        * * *       *    *       *   *  **   
  *   *  *   *  *      *        *       * ***  ******   *****       *    ******  *   *  *  **
  ******************************************************************************************* 

说明

该程序有四个主要步骤:

  1. 读取输入字符串并将其拆分为各个部分。
  2. 建立一个2列矩阵,其中每行描述在适当方向上的单位位移。例如,[0 -1]表示向左移动一个步骤。第一行是路径的原点,[1 1]
  3. 计算沿第一维该矩阵的累积和。现在,每一行都描述了的坐标*。归一化为最小值1
  4. 创建一个新的矩阵,1该矩阵包含第3步中矩阵所指示的坐标,0否则。然后将其转换为char矩阵。

码:

1th                         % row vector [1 1]. Initiallize matrix of step 2
j                           % (step 1) read input string 
'.\d+'XX                    % split into components. Creates cell array of substrings
"                           % (step 2) for each component
   @Z)XK                    % unbox to obtain substring and copy
   6L)U:                    % obtain number and build vector of that size
   "                        % repeat as many times as that number
      K1)                   % paste substring. Get first character: '^', 'v', '>', '<'
      XK118=K94=-           % vertical component of unit displacement: -1, 0 or 1
      K62=K60=-             % horizontal component of unit displacement: -1, 0 or 1
      h                     % concatenate horizontally
      v                     % append vertically to existing matrix
   ]                        % end
]                           % end
Ys                          % (step 3) cumulative sum along first dimension
tY)X<1--                    % normalize to minimum value 1
lqg                         % (step 4) build matrix with 0/1
10*32+c                     % replace 0 by space and 1 by asterisk

它适用于最后一个示例吗?
nicael '16

多好!是的,它确实。我已编辑答案以包含该答案
Luis Mendo

8

JavaScript(ES6),204 211 210

编辑1错误修复-输出'*'表示无效输入
编辑2简化对x和y方向的解码

这是我对《藏宝图》的回答,已修改以符合规格。

F=m=>(m.replace(/\D(\d+)/g,(d,z)=>{for(;z--;r=[...r],r[x]=m,p[y]=r.join``)for(d<'>'?--x:d<'^'?++x:d<'v'?--y:++y,p=~x?~y?p:[y=0,...p]:p.map(r=>' '+r,x=0),r=p[y]||'';!r[x];)r+=' '},x=y=0,p=[m='*']),p.join`
`)

少打高尔夫球,或多或少解释

f=m=>(
  x=y=0, // starting position
  p=['*'], // output string array (initialized with minimum output)
  m.replace( /\D(\d+)/g, 
  (d,z) => // execute the following for each group direction/length. Length in z, direction in d[0]
  {
    while( z--)  // repeat for the len
    {
      // check d to change values of x and y
      // all the comparison are with > and <, not equal
      // so that they work with the whole d, not just d[0]
      d<'>'?--x:d<'^'?++x:d<'v'?--y:++y,
      // now if x or y are < 0 then p must be adjusted  
      p = ~x 
        ? ~y
          ? p // both x and y are >= 0, p is not changed
          : [y = 0, ...p] // y < 0, shift p by on adding a 0 element and set y to 0
        : p.map(r=> ' ' + r, x = 0); // x < 0, add a space to the left for each row in p and set x to 0
      r = p[y] || ''; // get current row in r
      for( ; !r[x]; ) // if the current row is empty or too short
        r += ' '; // ... add spaces up to position x
      // set character in x position
      r = [...r], // the shorter way is converting to array ...
      r[x] = '*', // setting the element
      p[y] = r.join`` // and the back to string using join
    }
  }),
  p.join`\n` // return output array as a newline separated string
}

测试

F=m=>(m.replace(/\D(\d+)/g,(d,z)=>{for(;z--;r=[...r],r[x]='*',p[y]=r.join``)for(d<'>'?--x:d<'^'?++x:d<'v'?--y:++y,p=~x?~y?p:[y=0,...p]:p.map(r=>' '+r,x=0),r=p[y]||'';!r[x];)r+=' '},x=y=0,p=['*']),p.join`
`)

// TEST
console.log = x => O.textContent += x + '\n';

console.log(F('')+'\n')

console.log(F('v6>4^3<7')+'\n')

console.log(F('^2v2>3<3v3>4^5v5>3^5>4v2<4v3>4^3v3>3^5>4v2<4v3>7^5>4v2<4v3>9^3<2^2v2>4^2v2<2v3>8^5>2v4>2^4v5<3>6^5>5<5v2>5<5v2>5<4v1>8^3<1^2v2>1v2>2^3v3>2^2>1^2v2<1v3<3>11^3<2^2v2>4^2v2<2v3>5^5>5<5v2>5<5v2>5<4v1>7^5>4v2<4v3>4^3v3>3^5>4v2<3v1<1v2>3^1>1v1'))
<pre id=O></pre>


似乎工作正常。
nicael

唯一的例外:当输入为空时,要求是write *
nicael 2015年

对于那些浏览器在解释ES6时遇到问题的人:jsfiddle.net/2vrrd1wt
nicael 2015年

@nicael感谢您的关注。固定为1个字节
edc65'1

1

Perl,174个字节

@M=(['*']);pop=~s/(.)(\d+)/($z=ord$1)&64?($y+=$z&8?-1:1)<0&&unshift@M,[$y++]:($x+=($z&2)-1)<0?@M=map{[$x=0,@$_]}@M:0,$M[$y][$x]='*'for1..$2/gre;print map{map{$_||$"}@$_,$/}@M

期望输入作为命令行参数。请务必引用该论点!
例:perl 177.pl "<1^2>3v4<5^6>7v8<9^10>11"

有点可读:

@M=(['*']);                                 # output origin/starting position

pop=~                                       # apply regex to cmdline arg
s!(.)(\d+)!                                 # match $1=direction, $2=count

    ($z=ord$1)                              # get ASCII code for char
    &64                                     # 'v^' have this bit set, '<>' don't

        ?                                   # adjust y:
            ($y += $z&8 ? -1 : 1)           # '^' has bit set, 'v' doesn't
            < 0 &&                          # negative y?
            unshift @M, [$y++]              # prepend row; abuse [] for $y++ saving 3 bytes

        :                                   # adjust x:
            ($x+= ($z&2) -1 )               # '>' has bit set: 2-1=1, '<' hasn't: 0-1=-1
            < 0 ?                           # negative x?
                @M = map{ [$x=0,@$_] } @M   # prepend column, reset x
                :0                          # '?:0' shorter than '&&()'
        ,                                   # oh, and also:
        $M[$y][$x]='*'                      # output current position.

    for 1..$2                               # iterate count
!grex;                                      

print map{ map{$_||$"} @$_, $/ } @M         # iterate rows/cols, print '*' or space

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.