パスをたどる


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)最後のコマンドの最後の3つのステップがパス全体を右にシフトすることに注意してください。入力: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
insertusernamehere

参照実装を提供してもらえますか?
LegionMammal978

それは実際にはだまされていませんか?決定してください:D
nicael

@nicael:そうではないと言うでしょう。I / Oは異なり、パスは他のパスを通過できないようです。これは、さまざまなゴルフ戦略を可能にする方法でよりシンプルだと思います。しかし、私はこれらをすべて自分で突然再開できることに気づかず、ただ票を投じているだけだと思いました。
マリヌ

@marinus OK だから、関連:Yarr!隠された宝物への地図!
ニカエル

回答:


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
  *   *  *****  *****  *****  *   *     *** *  ******  *     *    *   *  ******  *****  *****
  *   *  *   *  *   *  *   *  *   *     * * *  *       *  *  *    *   *  *       *   *  *   *
  *****  *****  *****  *****  *****     * * *  ******  ** * **    *****  ******  *****  *****
  *   *  *   *  *      *        *       * * *  *        * * *       *    *       *   *  **   
  *   *  *   *  *      *        *       * ***  ******   *****       *    ******  *   *  *  **
  ******************************************************************************************* 

説明

このプログラムには4つの主要なステップがあります。

  1. 入力文字列を読み取り、そのコンポーネントに分割します。
  2. 各行が適切な方向の単位変位を表す2列のマトリックスを作成します。たとえば[0 -1]、左に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

最後の例で機能しますか?
ニカエル

いいね!はい、そうです。回答を編集して、それを含めるようにしました
ルイスメンドー

8

JavaScript(ES6)、204 211 210

編集1バグ修正-void 入力の出力「*」
編集2 xおよびy diffへの方向の単純なデコード

これが仕様を満たすために修正された宝の地図私の答えです。

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>


完全に動作するようです。
ニカエル

1つだけ例外があります。入力が空の場合、要件はを書き込むこと*です。
ニカエル

ブラウザがES6の解釈に問題がある場合:jsfiddle.net/2vrrd1wt
ニカエル

@nicael気づいてくれてありがとう。1バイトの修正
-edc65

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

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.