অবাক, আপনি scallywags!


10

ব্ল্যাকবার্ড 18 তম শতাব্দীর গোড়ার দিকে একটি ইংরেজ জলদস্যু ছিল । যদিও তিনি লুটপাট ও জাহাজ চালানোর জন্য পরিচিত ছিলেন, তবুও তিনি তাঁর জাহাজগুলি তাদের ক্রুদের অনুমতিতে আদেশ করেছিলেন। তাঁর বন্দীদের ক্ষতি বা হত্যার কোনও খবর পাওয়া যায় নি accounts

এই চ্যালেঞ্জ কুখ্যাত ব্ল্যাকবার্ডের সম্মানে এবং আন্তর্জাতিক টক লাইক পাইরেট দিবসে অনুপ্রেরণা (19 সেপ্টেম্বর)। এছাড়া বিপরীত হয় এই প্রতিদ্বন্দ্বিতা দ্বারা Pyrrha


চ্যালেঞ্জ

এমন একটি প্রোগ্রাম তৈরি করুন যা কোনও ধরণের মানচিত্রকে ইনপুট হিসাবে গ্রহণ করে (নীচে তালিকাভুক্ত অক্ষরের সমন্বয়ে) এবং এর দিকনির্দেশগুলি আউটপুট করে।


ইনপুট

সকল ইনপুট গঠিত হবে v, >, <, ^, হোয়াইটস্পেস, এবং একটি একক X

আপনি নিম্নলিখিত অনুমান করতে পারেন:

  • মানচিত্রটি কখনই লুপ বা ক্রস করবে না

  • প্রারম্ভিক তীরটি সর্বদা বামতম কলামের নীচেরতম অক্ষর হবে

  • সর্বদা একটি ধন থাকবে ( X)

একটি নমুনা ইনপুট নীচে প্রদর্শিত হয়।

  >>v   >>>>>>v
  ^ v   ^     v
  ^ v   ^   v<<
  ^ v   ^   v
  ^ >>>>^   >>X
  ^
>>^

আউটপুট

আউটপুটটি ", "দিকনির্দেশের একটি- সীমাবদ্ধ স্ট্রিং হওয়া উচিত । নীচে উপরের মানচিত্র থেকে সঠিক আউটপুট দেওয়া আছে।

E2, N6, E2, S4, E4, N4, E6, S2, W2, S2, E2

একটি একক পেছনের নতুন লাইন বা স্থান অনুমোদিত।


উদাহরণ

In:
>>>>>>>>>>>>>>>v
               v
               v
               >>>>X

Out:
E15, S3, E4

In:
>>>>>>v
^     v
^     >>>>X

Out:
N2, E6, S2, E4

In:
X
^
^
^

Out:
N3

In:
>>>>>>v
^     v
^     v
      v
      >>>>>>X

Out:
N2, E6, S4, E6

In:
 X
 ^
 ^
>^

Out:
E1, N3

In:
>X

Out:
E1

In:
v<<<<<
vX<<<^
>>>>^^
>>>>>^

Out:
E5, N3, W5, S2, E4, N1, W3

জলদস্যু দিবসের মতো হ্যাপি ইন্টারন্যাশনাল টক!


বাম-সর্বাধিক কলামে একাধিক ডান-নির্দেশিত তীর রয়েছে এমন উদাহরণ আপনি অন্তর্ভুক্ত করতে চাইতে পারেন, যেখানে পাথটি প্রথম কলামে ফিরে আসে। সেক্ষেত্রে পথটির সূচনা শনাক্ত করা কিছুটা জটিল।
রেটো কোরাডি

আমি আপনার অনুরোধ অনুযায়ী একটি উদাহরণ যুক্ত করেছি। আমি বিশদটিও যোগ করেছি যে শুরুর চরিত্রটি কলামের নীচে থাকবে। @ রিটোকোরাডি
জ্যাচ গেটস

যেহেতু আমি শেষ বিভাগটির দৈর্ঘ্য সম্পর্কে সম্পর্কিত প্রশ্নে কিছুটা উদ্বেগ উত্থাপন করেছি, তাই আমি আবার নীটপিক করব এবং বলব যে এটি ঠিক এখানে বিপরীত প্রশ্ন নয়। কেউ আবার জলদস্যুদের আরও একবার চালানোর চেষ্টা করছেন, আমার ধারণা।
coredump

পার্থক্য হ'ল শেষ দিকের ধাপ গণনা। @ কর্ডাম্প কমপক্ষে, যতদূর আমি বলতে পারি।
জ্যাচ গেটস

1
@ জ্যাচগেটস হ্যাঁ, ঠিক (এবং কেবল পরিষ্কার বলতে গেলে, আমি বলছি না যে প্রশ্নটি পরিবর্তন করা উচিত, এটি বর্তমানে যেমন রয়েছে তেমন ভাল)।
coredump

উত্তর:


3

সিজেম, 78 বাইট

qN/_:,$W=:Tf{Se]}s:U,T-{_U="><^vX"#"1+'E1-'WT-'NT+'S0"4/=~_@\}g;;]e`{(+}%", "*

এটি অনলাইনে চেষ্টা করুন

ব্যাখ্যা

এখানে মূল ধারণাটি হ'ল দীর্ঘতম লাইনটি (আমরা এই দৈর্ঘ্যটি কল করব T), তারপরে সমস্ত লাইনকে একই দৈর্ঘ্যে প্যাড করুন এবং তাদেরকে সংযুক্ত করুন (এই নতুন স্ট্রিংটি হ'ল U)। এইভাবে মানচিত্রে সরানোর জন্য কেবল একটি একক কাউন্টারের প্রয়োজন। যোগ / বিয়োগের 1অর্থ একই লাইনে ডান / বাম Tদিকে সরে যাওয়া , যোগ করা / বিয়োগের অর্থ এক লাইনের উপরে / উপরে চলে যাওয়া।

qN/    e# Split the input on newlines
_:,    e# Push a list of the line lengths
$W=:T  e# Grab the maximum length and assign to T
f{Se]} e# Right-pad each line with spaces to length T
s:U    e# Concatenate lines and assign to U

এখন এটি লুপ সেট আপ করার সময়।

,T-    e# Push len(U) - T
       e# i.e. position of first char of the last line
{...}g e# Do-while loop
       e# Pops condition at the end of each iteration

লুপের বডিটি একটি অনুসন্ধান সারণী ব্যবহার করে এবং কী করতে হবে তা বেছে নিতে ইভিএল। প্রতিটি পুনরাবৃত্তির শুরুতে শীর্ষ স্ট্যাক উপাদানটি বর্তমান অবস্থান। এর নীচে সমস্ত প্রক্রিয়াজাত এনএসডব্লিউই দিকনির্দেশ রয়েছে। পুনরাবৃত্তির শেষে, নতুন দিকটি অবস্থানের নীচে স্থাপন করা হয় এবং এর একটি অনুলিপি লুপের শর্ত হিসাবে ব্যবহৃত হয়। শূন্য-অক্ষরগুলি সত্য y যখন কোনও এক্স এর মুখোমুখি হয়, 0 টি লুপটি সমাপ্ত করে, দিক হিসাবে ধাক্কা দেয়।

_U=      e# Push the character in the current position
"><^vx"# e# Find the index in "><^Vx"
"..."4/  e# Push the string and split every 4 chars
         e# This pushes the following list:
         e# [0] (index '>'): "1+'E" pos + 1, push 'E'
         e# [1] (index '<'): "1-'W" pos - 1, push 'W'
         e# [2] (index '^'): "T-'N" pos - T, push 'N'
         e# [3] (index 'v'): "T+'S" pos + T, push 'S'
         e# [4] (index 'X'): "0"    push 0
=~       e# Get element at index and eval
_@\      e# From stack: [old_directions position new_direction]
         e# To stack: [old_directions new_direction position new_direction]
         e# (You could also use \1$)
         e# new_direction becomes the while condition and is popped off

এখন স্ট্যাক সৌন্দর্য এটি পছন্দ: [directions 0 position]। এর আউটপুট জেনারেট করা যাক।

;;    e# Pop position and 0 off the stack
]     e# Wrap directions in a list
e`    e# Run length encode directions
      e# Each element is [num_repetitions character]
{     e# For each element:
 (+   e#   Swap num_repetitions and character
}%    e# End of map (wraps in list)
", "* e# Join by comma and space

3

সিজেম, 86 বাইট

qN/_,{1$=cS-},W=0{_3$3$==_'X-}{"^v<>"#_"NSWE"=L\+:L;"\(\ \)\ ( )"S/=~}w];Le`{(+}%", "*

এটি অনলাইনে চেষ্টা করুন

ব্যাখ্যা:

qN/     Get input and split into rows.
_,      Calculate number of rows.
{       Loop over row indices.
  1$=     Get row at the index.
  c       Get first character.
  S-      Compare with space.
},      End of filter. The result is a list of row indices that do not start with space.
W=      Get last one. This is the row index of the start character.
0       Column number of start position. Ready to start tracing now.
{       Start of condition in main tracing loop.
  _3$3$   Copy map and current position.
  ==      Extract character at current position.
  _'X-    Check if it's the end character `X.
}       End of loop condition.
{       Start of loop body. Move to next character.
  "^v<>"  List of directions.
  #       Find character at current position in list of directions.
  _       Copy direction index.
  "NSWE"  Matching direction letters.
  =       Look up direction letter.
  L\+:L;  Append it to directions stored in variable L.
  "\(\ \)\ ( )"
          Space separated list of commands needed to move to next position for each of
          the 4 possible directions.
  S/      Split it at spaces.
  =       Extract the commands for the current direction.
  ~       Evaluate it.
}w      End of while loop for tracking.
];      Discard stack content. The path was stored in variable L.
Le`     Get list of directions in variable L, and RLE it.
{       Loop over the RLE entries.
  (+      Swap from [length character] to [character length].
}%      End of loop over RLE entries.
", "*   Join them with commas.

2

জাভাস্ক্রিপ্ট (ES6), 239 বাইট

a=>(b=a.split`
`,b.reverse().some((c,d)=>c[0]!=' '&&((e=d)||1)),j=[],eval("for(g=b[e][f=0];b[e][f]!='X';g=b[e][f],j.push('NSEW'['^v><'.indexOf(i)]+h))for(h=0;g==b[e][f];e+=((i=b[e][f])=='^')-(i=='v'),f+=(i=='>')-(i=='<'),h++);j.join`, `"))

ব্যাখ্যা:

a=>(
    b = a.split('\n'),
    // loops through list from bottom to find arrow
    b.reverse().some(
        (c, d)=>
            // if the leftmost character is not a space, saves the index and exit
            // the loop
            // in case d == 0, the ||1 makes sure the loop is exited
            c[0] != ' ' && ((e = d) || 1)
    ),
    j = [], // array that will hold the instructions
    eval("  // uses eval to allow a for loop in a lambda without 'return' and {}

        // loops through all sequences of the same character
        // e is the first coordinate of the current character being analyzed
        // f is the second coordinate
        // defines g as the character repeated in the sequence
        // operates on reversed b to avoid using a second reverse
        // flips ^ and v to compensate

        for(g = b[e][f = 0];
            b[e][f] != 'X'; // keep finding sequences until it finds the X
            g = b[e][f],    // update the sequence character when it hits the start of a
                            // new sequence
            j.push('NSEW'['^v><'.indexOf(i)] + h)) // find the direction the sequence is
                                                   // pointing to and add the
                                                   // instruction to j

            // loops through a single sequence until it hits the next one
            // counts the length in h
            for(h = 0;
                g == b[e][f]; // loops until there is a character that isn't part of
                              // the sequence
                // updates e and f based on which direction the sequence is pointing
                // sets them so that b[e][f] is now the character being pointed toward
                e += ((i = b[e][f]) == '^') - (i == 'v'),
                f += (i == '>') - (i == '<'),
                // increments the length counter h for each character of the sequence
                h++);

            // return a comma separated string of the instructions
            j.join`, `
    ")
)

0

জাভাস্ক্রিপ্ট (ES6), 189

নীচে স্নিপেট চলমান পরীক্ষা করুন একমাস্ক্রিপ্ট comp অনুবর্তী ব্রাউজারে।

f=m=>{(m=m.split`
`).map((r,i)=>r[0]>' '?y=i:0);for(x=o=l=k=p=0;p!='X';++l,y+=(k==1)-(k<1),x+=(k>2)-(k==2))c=m[y][x],c!=p?(o+=', '+'NSWE'[k]+l,l=0):0,k='^v<>'.search(p=c);alert(o.slice(7))}

// Testable version, no output but return (same size)
f=m=>{(m=m.split`
`).map((r,i)=>r[0]>' '?y=i:0);for(x=o=l=k=p=0;p!='X';++l,y+=(k==1)-(k<1),x+=(k>2)-(k==2))c=m[y][x],c!=p?(o+=', '+'NSWE'[k]+l,l=0):0,k='^v<>'.search(p=c);return o.slice(7)}


// TEST
out = x => O.innerHTML += x+'\n';

test = 
[[`>>>>>>>>>>>>>>>v
               v
               v
               >>>>X`,'E15, S3, E4']
,[`>>>>>>v
^     v
^     >>>>X`,'N2, E6, S2, E4']
,[`X
^
^
^`,'N3']
,[`>>>>>>v
^     v
^     v
      v
      >>>>>>X`,'N2, E6, S4, E6']
,[` X
 ^
 ^
>^`,'E1, N3']
,[`>X`,'E1']
,[`v<<<<<
vX<<<^
>>>>^^
>>>>>^`,'E5, N3, W5, S2, E4, N1, W3']];

test.forEach(t=>{
  var k = t[1];
  var r = f(t[0]);
  out('Test ' + (k==r ? 'OK' : 'Fail')
      +'\n'+t[0]+'\nResult: '+r
      +'\nCheck:  '+k+'\n');
})
<pre id=O></pre>

কম গল্ফড

f=m=>{
  m=m.split('\n'); // split in rows

  x = 0; // Starting column is 0
  m.forEach( (r,i) => r[0]>' '? y=i : 0); // find starting row

  o = 0; // output string
  p = 0; // preceding character
  l = 0; //  sequence length (this starting value is useless as will be cutted at last step)
  k = 0; //  direction (this starting value is useless as will be cutted at last step)
  while(p != 'X') // loop until X found
  {
    c = m[y][x]; // current character in c
    if (c != p) // changing direction
    {  
      o +=', '+'NSWE'[k]+l; // add current direction and length to output
      l = 0 // reset length
    );
    p = c;
    k = '^v<>'.search(c); // get new current direction
    // (the special character ^ is purposedly in first position)
    ++l; // increase sequence length
    y += (k==1)-(k<1); // change y depending on direction
    x += (k>2)-(k==2); // change x depending on direction
  }
  alert(o.slice(7)); // output o, cutting the useless first part
}
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.