梯子上的机器人


30

背景

我有一个倚在墙上的梯子,还有一个可以爬上它的遥控机器人。我可以向机器人发送三个不同的命令:

  • UP:机器人向上走了一步。如果在最高的台阶上,它会跳下,跌落并爆炸。
  • DOWN:机器人向下走了一步。如果它处于最低位置,则什么也不会发生。
  • RESET:机器人返回到最低位置。

我还可以发送一系列命令,机器人将一个接一个地执行它们。您的任务是预测其运动。

输入值

您的输入是一个正整数N,代表阶梯中的步数;一个非空字符串Cover UDR,代表我已经发送给机器人的命令。您可以假设N < 1000。机器人在梯子的最低位置初始化。

输出量

可以保证机器人会在某个时候爬上最高的台阶并爆炸。您的输出是在此之前执行的命令数。

考虑输入N = 4C = "UDDUURUUUUUUUDDDD" 所述的机器人,由表示@沿4-阶梯,移动如下:

|-|   |-|   |-|   |-|   |-|   |-|   |-|   |-|   |-|   |@|   |-||
|-|   |-|   |-|   |-|   |-|   |@|   |-|   |-|   |@|   |-|   |-||
|-|   |@|   |-|   |-|   |@|   |-|   |-|   |@|   |-|   |-|   |-|v
|@| U |-| D |@| D |@| U |-| U |-| R |@| U |-| U |-| U |-| U |-|# Boom!

由于机器人已爆炸,其余命令将不执行。爆炸发生在10个命令之后,因此正确的输出是10

规则和计分

您可以编写完整的程序或函数。最低字节数获胜,并且不允许出现标准漏洞。

测试用例

  1 U -> 1
  1 DDRUDUU -> 4
  4 UDDUUUUURUUUUDDDD -> 7
  4 UDDUURUUUUUUUDDDD -> 10
  6 UUUUUDRUDDDDRDUUUUUUDRUUUUUUUDR -> 20
 10 UUUUUURUUUUUUURUUUUUUUURUUUUUUUUUUUUUU -> 34
  6 UUUDUUUUDDDDDDDDDDDDDDRRRRRRRRRRRUUUUUU -> 8
  6 UUUDUUUDURUDDDUUUUUDDRUUUUDDUUUUURRUUDDUUUUUUUU -> 32
 20 UUDDUDUUUDDUUDUDUUUDUDDUUUUUDUDUUDUUUUUUDUUDUDUDUUUUUDUUUDUDUUUUUUDUDUDUDUDUUUUUUUUUDUDUUDUDUUUUU -> 56
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UUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUUU -> 872


8
我很失望,任务不是生成这种ASCII艺术。
user253751'3


除了字符串,我们还可以列出字符代码吗?
Cyoce

@Cyoce仅当您的语言没有其他方式可以表示字符串时。
Zgarb

Answers:


10

CJam,26 25 22字节

0l{i"()~ "=~0e>_}%ri#)

输入格式是第一行的说明,第二行的梯子高度。

在这里测试。

说明

0         e# Push a 0 - the initial position of the robot.
l         e# Read the instructions.
{         e# Map this block over the instruction...
  i       e#   Convert to character code. D -> 68, U -> 85, R -> 82.
  "()~ "= e#   We use these as cyclic indices into this array. Note that the values
          e#   modulo 4 are 0, 1 and 2, respectively. So U -> ) (increment),
          e#   D -> ( (decrement), R -> ~ (bitwise NOT, i.e negated and decrement).
  ~       e#   Evaluate the character as code.
  0e>     e#   Clamp to non-negative numbers. So D can't go below 0, and since R is
          e#   guaranteed to yield something negative, this resets it to zero.
  _       e#   Duplicate, so we keep one copy of the current position on the stack.
}%        e# Since this was a map, the result will be wrapped in an array.
ri        e# Read the ladder height and convert it to an integer.
#         e# Find its first occurrence in the list of positions.
)         e# The result is off by one, so we increment it.

即使爆炸后也可以处理所有命令的好主意。我会借用它来节省一些字节
Luis Mendo

7

C,83 71 + 4 = 75字节

感谢@Josh向我展示K&S样式,该样式允许关闭8个字节!

f(i,s,z,a)char*s;{z-=*s<82?z>0:*s>82?-1:z;return z-i?f(i,s+1,z,a+1):a;}

说明:

f(i,s,z,a)char*s;{ // function needs one integer and one "string"
  z-=              // z is the bot's height
    *s<82?         // if 'Down'
      z>0          // then subtract 1 when z>0 or nothing otherwise
    :*s>82?        // else if 'Up'
      -1           // increase height z-=-1
    :z;            // else reset z=z-z
  return z-i?      // if z is not the max height
    f(i,s+1,z,a+1) // try next step
  :a;              // else print how many calls/steps were made
}                  // end of function

示例调用:

f(1,"U",0,1);    // I've added 4 bytes in the score because of ",0,1"

ideone现场测试


1
答案很不错,但是值得注意的是,由于全局变量,该功能只能有效使用一次za并且不会重置。
乔什(Josh)

@乔什 我已经更新了。:)
删除

1
太棒了!您还可以通过在函数中使用类型声明来节省几个字符:codegolf.stackexchange.com/a/40266/13877
Josh

@乔什 哇,太棒了!谢谢
删除

6

JavaScript(ES6),54 53字节

n=>c=>(f=p=>n-p?f({D:p&&p-1,U:p+1}[c[i++]]|0):i)(i=0)

说明

在内部使用递归函数。

var solution =

n=>c=>(
  f=p=>             // f = recursive function, p = position of robot on ladder
    n-p?            // if p != n
      f({           // execute the next command
          D:p&&p-1, // D -> p = max of p - 1 and 0
          U:p+1     // U -> p = p + 1
        }[c[i++]]   // get current command then increment i (current command index)
        |0          // R -> p = 0
      )
    :i              // else return the current command index
)(i=0)              // initialise p and i to 0 for the first recurse
N = <input type="number" id="N" value="354" /><br />
C = <input type="text" id="C" value="UUDDUUDUDUUDDUDUUUUDDDUDUUDUDUDUDDUUUUDUDUUDUDUUUDUDUDUUDUUUDUUUUUDUUDUDUUDUDUUUUUDUDUUDUDUDUDDUUUUUUUDUDUDUDUUUUUDUDUDUDUDUDUDUDUUDUUUUUURUUUDUUUUDDUUDUDUDURURURUDUDUUUUDUUUUUUDUDUDUDUDUUUUUUDUDUUUUUUUDUUUDUUDUDUDUUDUDUDUUUUUUUUUUDUUUDUDUUDUUDUUUDUUUUUUUUUUUUUDUUDUUDUDUDUUUDUDUUUUUUUDUUUDUUUDUUDUUDDUUUUUUUUDUDUDUDUDUUUUDUDUUUUUUUUDDUUDDUUDUUDUUDUDUDUDUUUUUUUUUDUUDUUDUUUDUUDUUUUUUUUUUUDUDUDUDUUUUUUUUUUUUDUUUDUUDUDDUUDUDUDUUUUUUUUUUUUDUDUDUUDUUUDUUUUUUUDUUUUUUUUUDUDUDUDUDUUUUUUDUDUDUUDUDUDUDUUUUUUUUUUUUUUUDUDUDUDDDUUUDDDDDUUUUUUUUUUUUUUDDUDUUDUUDUDUUUUUUDUDUDUDUDUUUUDUUUUDUDUDUUUDUUDDUUUUUUUUUUUUUUUUUUDUUDUUDUUUDUDUUUUUUUUUUUDUUUDUUUUDUDUDUUUUUUUUUDUUUDUUUDUUDUUUUUUUUUUUUDDUDUDUDUUUUUUUUUUUUUUUDUUUDUUUUDUUDUUDUUUUUUUUUUUDUDUUDUUUDUUUUUUDUDUDUUDUUUUUUUUUUUUDUUUDUUDUDUDUUUUDUDUDUDUDUUUUUUUUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUUU" /><br />
<button onclick="result.textContent=solution(+N.value)(C.value)">Go</button>
<pre id="result"></pre>


4

Perl,47 + 2 = 49字节

$z-=-/U/||$z&&/D/;$z*=!/R/;$^I<=$z&&last}{$_=$.

需要-p标志,-i$N用于后面的高度和换行符分隔的移动列表:

$ perl -pi10 ladderbot.pl <<<< $'U\nU\nU\nU\nU\nU\nR\nU\nU\nU\nU\nU\nU\nU\nR\nU\nU\nU\nU\nU\nU\nU\nU\nR\nU\nU\nU\nU\nU\nU\nU\nU\nU\nU\nU\nU\nU\nU'
34

怎么运行的:

                                                # '-p' wraps the code in (simplified):
                                                # while($_=<>) {...print $_}
$z-=-/U/||$z&&/D/;                              # Subtract -1 if UP. subtract 1 if DOWN
                  $z*=!/R/;                     # If R then times by zero
                           $^I<=$z&&last        # Break while loop if N is reached
                                        }{      # Eskimo operator:
                                                # while($_=<>){...}{print$_}
                                          $_=$. # `$.` contains number of lines read from input.

失望:

LINE: while (defined($_ = <ARGV>)) {
    $z -= -/U/ || $z && /D/;
    $z *= !/R/;
    last if $^I <= $z;
}
{
    $_ = $.;
}
continue {
    die "-p destination: $!\n" unless print $_;
}
-e syntax OK

4

JavaScript(SpiderMonkey 30 +),65 64字节

(n,s,i=0)=>[for(c of s)if(i<n)c<'E'?i&&i--:c>'T'?i++:i=0].length

怎么运行的

我们首先将变量设置i为0。这将跟踪机器人爬了多少步。然后,对于c输入字符串中的每个字符,我们运行以下逻辑:

  1. 如果i大于或等于n,则什么也不做。
  2. 如果c"D"
    • 如果i为0,则保持原样。
    • 否则,将其递减1。
  3. 如果c"U",则增加i1。
  4. 否则,设置i为0。

通过截断if i>=n,我们避免了机器人到达顶部后再向阵列添加任何项目。因此,我们可以简单地返回结果数组的长度。


3

Haskell,65个字节

x%'U'=x+1
x%'D'=max(x-1)0
x%_=0
f n=length.fst.span(<n).scanl(%)0

用法示例:f 4 "UDDUURUUUUUUUDDDD"-> 10

%调整梯子上的当前位置,scanl列出所有位置,fst.span(<n)在爆炸之前参与零件并length计算步数。


结合命名论点和成分/ currying
一起做的很好


3

MATL37 34字节

Oj"t@4\1=?Q}6M?x0}qt0>*]]]N$h=f1)q

在线尝试!

说明

排名是从0开始的。将每个新位置推入堆栈,并保留旧位置。因此,堆栈大小代表到目前为止的移动数加1。

循环用于处理每个命令。当位置达到梯子高度时,即使爆炸后,该循环也会退出,处理所有命令(想法取自Martin的答案)。最终结果由等于梯子高度的第一个位置的索引给出。

O             % push a 0: initial position (0-based)
j             % take first input (commands) as a string
"             % for each command
  t           %   duplicate current position
  @           %   push command
  4\          %   modulo 4. This gives 1, 2, 0 for 'U','R', 'D'
  1=?         %   if result equals 1 (command 'U')
    Q         %     increase position by 1
  }           %   else
    6M?       %     if result was nonzero (command 'R')
      x0      %       delete current position and push 0
    }         %     else (command 'D')
      q       %       decrement by 1
      t0>*    %       turn negative values into 0
    ]         %     end if
  ]           %   end if
]             % end for each
N$h           % pack all numbers in the stack into an array
=             % implicitly read second input: ladder height
f1)q          % find first position equal to that, and subtract 1.
              % Implicitly display

3

Python 2,63 62字节

f=lambda n,s,h=0:h^n and-~f(n,s[1:],-[2%~h,~h,0][ord(s[0])%4])

例如,f(4, 'UDDUURUUUUUUUDDDD')10

xnor发现了一个更短的表达式:2%~h真的很酷:)


很高兴找到%4。如果我没记错的话,您可以这样做来保存字符-[2%~h,~h,0][ord(s[0])%4]
xnor

3

PowerShell,86 79字节

param($a,[char[]]$b)$b|%{$d++;if(($c-=((1,0)[!$c],-1,$c)[$_%4])-ge$a){$d;exit}}

我的圣诞老人何时进入地下室进行了稍微的改装回答。

接受input $a$b,显式转换$b为char数组。然后,我们遍历|%{...}所有$b。每次迭代,我们都会增加计数器$d

然后,if声明以检查我们是否已经达到了顶峰-ge$a。如果是这样,我们输出$dexit。该if语句由伪三元构造而成,该伪三元是通过向$c数组中的多个索引的结果分配负等于来创建的。

我们有一个把戏的ASCII值DR以及U对应于021当取模-4,所以$_%4作为我们的第一个指标。如果为R,则设置$c等于$c-$c,进行重置。如果为U,则意味着我们需要上升,因此$c-(-1)结果为。否则它是一个D,因此我们需要检查是否已经在底部(!$c在PowerShell中为-,“ not-zero”是“ true”或1),并分别设置$c$c-0$c-1

编辑-通过使用负等号分配而不是直接分配节省了7个字节


3

Perl 5,61个字节

包含的两个字节-F -i。(-M5.01免费)

整数(例如10)的输入为perl -M5.01 -F -i10 robot.pl;梯形图命令的输入为STDIN。

for(@F){($i+=/U/)-=/R/?$i:$i&&/D/;$j++;last if$^I<=$i}say$j

使用perl 5.12.5,我还必须显式启用autosplit模式,-anF然后才能为我打印任何内容。但似乎仅-F在5.20.3中隐式启用。你能验证一下吗?
ardnew

@ardnew,-F对我来说就足够了(5.20或5.22左右)。Iirc目前的perldoc perlrun说它暗含-a-a暗含-n
msh210 '16

我们算多少钱-i?我可以看到您将其算为1,但我想实际上应该算为3?:-)
andlrc '16

@ dev-null,为什么三个?我认为PPCG.SE的惯例是计算标记中的字母,而不是连字符号,但是如果我错了,请纠正我。(看来您也对这个问题使用了相同的计数约定,即是您自己的答案。(顺便说一句,很好的答案。))
msh210 '16

@ msh210我只是在计算使用-i和不使用perl -i10 -pe';'vs perl -pe';'超过3个字符的差异,然后是输入数字-我想我们不应该计算。但是,即使今天早上我也可能是错的:-)
andlrc '16

3

Vitsy,44个字节

可能会有一些减少-如果可以的话,我会找出更多的方法。

0vVWl:X[4M1+mDvV-);]lvXv?v-N
vD([1-]
v1+
vX0

说明(进行中):

0vVWl:X[4M1+mDvV-);]lvXv?v-N
0v             Save 0 as our temp var to edit.
  V            Save the input as a global var.
   W           Grab a line of STDIN.
    l          Push the length of the stack.
     :         Clone the stack. This is for later use.
      X        Remove the top item of the stack (we don't need the length right now.
[            ] Do the stuff in the brackets infinitely.
 4M            Modulo 4.
   1+          Add one.
     m         Go to the line index as specified by the top item of the stack.
      Dv       Duplicate the top item, save it in the temp var.
        V-);   If the top value is equal to the input number, exit the loop.
l              Push the length of the stack.
 vXv           Dump the temp var, then save the top item.
    ?          Rotate back to the original stack.
     v-        Subtract the top item (the original length) by the temp var (new length)
       N       Output the top item of the stack of the number.

vD([1-]
v              Push the temp variable to the stack.
 D([  ]        If this value is not zero...
    1-         Subtract one from it.

v1+            Push the temp variable to the stack, then add one to it.

vX0            Dump the temp var and replace it with zero.

在线尝试!(大测试用例)


2

PHP,88字节

这会产生一些通知(3 + 2n,其中n是运行的命令数)通知,但这对打高尔夫球无关紧要,对吧?

<?php for(;$x++<$argv[1];)switch($argv[2][$i++]){case R;$x=2;case D;--$x?--$x:0;}echo$i;

松散:

<?php                    # actually 1 byte shorter not as a function
for(;$x++<$argv[1];)     # not initialising the $x causes a notice but still works
                         # post increment $x by 1 regardless of the command (saves us writing a U case)
  switch($argv[2][$i++]) # get next command, increment number of commands
    {case R;             # R gets treated as a constant with value 'R'. and a notice
      $x=2;              # falling through to D which will double decrement so set to 2
    case D;              # same trick as R
      --$x?--$x:0;}      # decrement once then again if >0
echo$i;                  # output

只要代码仍然可以运行,通知就可以了。
Zgarb

2

Python,121字节

def f(a,n):
 i=c=0
 while i<n:i={'U':lambda x:x+1,'D':lambda x:0 if x==0 else x-1,'R':lambda x:0}[a[c]](i);c+=1
 return c

1
欢迎来到PPCG!在这里,我们默认要求答案是使用STDIN输入并打印到STDOUT的完整程序,或者是将输入作为参数并返回输出值的函数。您的解决方案对输入进行硬编码,这是不允许的。
Zgarb

通过形成函数进行纠正,编写代码时我正在使用解释器。
Alex Burge

谢谢!您还应该添加表格的标题,## Python, <N> bytes以向其他人显示您的分数。
Zgarb

我不认为可以有一个未命名的函数。
user48538

您可以替换0 if x==0 else x-1x and x-1
Cyoce

2

JavaScript中,131 106 Bytes-

我知道这不会赢得Code Golf比赛,但这是一个有趣而愚蠢的解决方案,可以实现:

l=>s=>Function('i=c=0;'+s.replace(/./g,x=>`c++;i${{R:"=0",U:`++;if(i>=${l})re‌​turn c`,D:"--"}[x]};`))()

我通过制作动态生成的命令式解决方案来与“功能”路线相反,将指令的任何实例替换为增量或减量以及计数器增量。

感谢Cycoce为我节省了29个字节!


在这里,我打了29个字节:l=>s=>Function('i=c=0;'+s.replace(/./g,x=>`c++;i${{R:"=0",U:`++;if(i>=${l})return c`,D:"--"}[x]};`))()
Cyoce

2

Python 3、90

DSM节省了6个字节。

现在非常简单。

def f(c,n):
 f=t=0
 for x in c:
  f+=1|-(x<'E');f*=(x!='R')&(f>=0);t+=1
  if f>=n:return t

测试用例:

assert f('U', 1) == 1
assert f('DDRUDUU', 1) == 4
assert f('UDDUUUUURUUUUDDDD', 4) == 7
assert f('UDDUURUUUUUUUDDDD', 4) == 10
assert f('UUDDUUDUDUUDDUDUUUUDDDUDUUDUDUDUDDUUUUDUDUUDUDUUUDUDUDUUDUUUDUUUUUDUUDUDUUDUDUU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UDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUUU', 354) == 872

1

PHP,129字节

function r($h,$s){$i=0;$c=1;while($x=$s[$i++]){if($x=='U'){$c++;}elseif($x=='D'){--$c<1?$c=1:0;}else{$c=1;}if($c>$h){return$i;}}}

不是赢,而是创造的乐趣。PHP似乎不喜欢三元运算符中的空白部分(它会引发语法错误),因此我不得不在其中放置一个0

非高尔夫版本:

function r($h,$s) {          // $h - height of ladder; $s - instructions
  $i = 0;                    // Instruction index
  $c = 1;                    // Position on ladder
  while ($x = $s[$i++]){     // Set $x to current instruction and increment index
    if ($x == 'U'){          // If instruction is U...
      $c++;                  // Increment ladder position
    } elseif ($x == 'D') {   // If instruction is D...
      --$c < 1 ? $c = 1 : 0; // Decrement ladder position, if under 1 set to 1
    } else {                 // If instruction is anything else (in this case R)
      $c = 1;                // Reset ladder position
    }
    if ($c > $h) {           // If ladder position is larger than height...
      return $i;             // Return current instruction index
    }
  }
}

1

PHP,113字节

较小版本的https://codegolf.stackexchange.com/a/74575/13216

function r($h,$s){$i=0;$c=1;while($x=$s[$i++]){$c+=($x=='U'?1:($x=='D'?($c>1?-1:0):1-$c));if($c>$h){return $i;}}}

取消高尔夫:

// $h - height of ladder; $s - instructions
function r($h,$s) {
    $i = 0;
    $c = 1;
    while ($x = $s[$i++]) {
        $c += (
            $x=='U'?
                1
            :
                (
                    $x=='D'? (
                        $c>1?
                            -1
                        :
                            0
                    ):
                        1-$c
                )
        );
        if ($c > $h) {
            return $i;
        }
    }
}

2
很棒的第一篇文章!我编辑了您的帖子以提高可读性。打高尔夫球快乐!
GamrCorps'3

1

Pyth,19个字节

x.u@[tWNNhN00)CYz0Q

在线尝试:演示测试套件

说明:

x.u@[tWNNhN00)CYz0Q   implicit: z = input string, Q = input number
 .u             z0    reduce z: for each char Y in z manipulate N = 0 with:
    [        )           create a list with
     tWNN                  * N-1 if N>0 else N
         hN                * N+1
           0               * 0
            0              * 0
   @          CY         replace N by the ord(Y)-th element (mod 4)
 .u                   .u give us a list with all intermediate values of N
x                 Q   print the index of Q in this list

1

Java,250个字节

int cmds(int n, String s) {
int steps=1;
int count=0;
for (int i=0;i< s.length();i++) {
count++;
char c=s.charAt(i);
switch(c){
case 'D':
steps=(steps==1)?1:--steps;
break;
case 'R':
steps=1;
break;
case 'U':
++steps;
break;
}
if(steps>n)
return count;
}
return 0;
}

2
回答代码高尔夫球挑战时,您应该以开始回答# <language_name>, XX bytes。另外,您可以将变量名每个减少为一个字符并删除多余的空格,这样您的字节数将减少(这是此处的目标)...啊,欢迎使用PPCG!
移除

一些提示:要将代码缩进为代码,请在该行的开头添加4个空格。您删除了一些空格,但仍然可以删除更多空格(例如:代替int steps=1; int count=0;您可以使用int s=1,c=0;- 看起来我更改了变量名 -依此类推)。您仍然可以在高尔夫球版本下方显示您的高尔夫球版本并附带说明(这种方法很容易被某人帮助您高尔夫球更多字节)。
删除了

1

C,91字节

没有警告gcc -Wall。递归和逗号分隔的表达式。

r.c 包含裸功能:

int r(int N,int n,int s,char*C){return*C&&s<=N?s+=*C&2?-s:*C&1?1:-1,r(N,n+1,s?s:1,C+1):n;}

评论过

int r(int N,   // number of steps on ladder
      int n,   // result, seed with 0
      int s,   // current step, seed with 1
      char *C  // command string
      )
{
    return *C&&s<=N ?  // still reading commands and still on ladder?
       s+=                // increment step value by...
        *C&2?             // bit test if 'R' but not 'U' or 'D'.
         -s               // negate to 0, will set to 1 in call if needed
         :*C&1?           // Not 'R', is it 'U'?
            1             // 'U', add 1
            :-1,          // Must be 'D', subtract 1
       r(N,n+1,s?s:1,C+1) // Recursive call, and fix case where s==0.
      :n;                 // end of string or fell off ladder
}

以供参考,

'U'.charCodeAt(0).toString(2)
"1010101"
'D'.charCodeAt(0).toString(2)
"1000100"
'R'.charCodeAt(0).toString(2)
"1010010"

roboladder.c 包装器

#include <stdio.h>
#include <stdlib.h>
#include "r.c"
int main(int argc, char * argv[])
{
  int N = atoi(argv[1]);
  int n = r(N,0,1,argv[2]);
  int check = atoi(argv[3]);
  printf("%d : %d\n", n, check);
  return 0;
}

Makefile 供测试用,

run:
    @gcc -Wall robotladder.c -o robotladder 
    @./robotladder 1 U 1
    @./robotladder 1 DDRUDUU 4  
    @./robotladder 4 UDDUUUUURUUUUDDDD 7
    @./robotladder 4 UDDUURUUUUUUUDDDD 10
    @./robotladder 6 UUUUUDRUDDDDRDUUUUUUDRUUUUUUUDR 20
    @./robotladder 10 UUUUUURUUUUUUURUUUUUUUURUUUUUUUUUUUUUU 34
    @./robotladder 6 UUUDUUUUDDDDDDDDDDDDDDRRRRRRRRRRRUUUUUU 8
    @./robotladder 6 UUUDUUUDURUDDDUUUUUDDRUUUUDDUUUUURRUUDDUUUUUUUU 32
    @./robotladder 20 UUDDUDUUUDDUUDUDUUUDUDDUUUUUDUDUUDUUUUUUDUUDUDUDUUUUUDUUUDUDUUUUUUDUDUDUDUDUUUUUUUUUDUDUUDUDUUUUU 56
    @./robotladder 354 UUDDUUDUDUUDDUDUUUUDDDUDUUDUDUDUDDUUUUDUDUUDUDUUUDUDUDUUDUUUDUUUUUDUUDUDUUDUDUUUUUDUDUUDUDUDUDDUUUUUUUDUDUDUDUUUUUDUDUDUDUDUDUDUDUUDUUUUUURUUUDUUUUDDUUDUDUDURURURUDUDUUUUDUUUUUUDUDUDUDUDUUUUUUDUDUUUUUUUDUUUDUUDUDUDUUDUDUDUUUUUUUUUUDUUUDUDUUDUUDUUUDUUUUUUUUUUUUUDUUDUUDUDUDUUUDUDUUUUUUUDUUUDUUUDUUDUUDDUUUUUUUUDUDUDUDUDUUUUDUDUUUUUUUUDDUUDDUUDUUDUUDUDUDUDUUUUUUUUUDUUDUUDUUUDUUDUUUUUUUUUUUDUDUDUDUUUUUUUUUUUUDUUUDUUDUDDUUDUDUDUUUUUUUUUUUUDUDUDUUDUUUDUUUUUUUDUUUUUUUUUDUDUDUDUDUUUUUUDUDUDUUDUDUDUDUUUUUUUUUUUUUUUDUDUDUDDDUUUDDDDDUUUUUUUUUUUUUUDDUDUUDUUDUDUUUUUUDUDUDUDUDUUUUDUUUUDUDUDUUUDUUDDUUUUUUUUUUUUUUUUUUDUUDUUDUUUDUDUUUUUUUUUUUDUUUDUUUUDUDUDUUUUUUUUUDUUUDUUUDUUDUUUUUUUUUUUUDDUDUDUDUUUUUUUUUUUUUUUDUUUDUUUUDUUDUUDUUUUUUUUUUUDUDUUDUUUDUUUUUUDUDUDUUDUUUUUUUUUUUUDUUUDUUDUDUDUUUUDUDUDUDUDUUUUUUUUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUDUUUUDUDUUUUUU 872
    @wc -c r.c

1

Mathematica,114120字节

(d=#~Max~1-1&;u=#+1&;r=1&;s=StringToStream@ToLowerCase@#;l=1;t=1;While[(l=ToExpression[s~Read~Character]@l)<=#2,t++];t)&

匿名函数,它接受两个参数(C,N)。请谨慎使用,因为它不会关闭打开的流。它还会全局分配所有变量。

修改为替换d=#-1&d=#~Max~1-1&,因此robie不会被挖掘。


等待:我认为这无效。它使机器人可以放下负梯级。哎呀 那将教会我进行不全面的测试...如果有机会,我会把更正的答案进行测试。
hYPotenuser

1

Mathematica,112字节

i=0;First@Position[ToExpression["{"<>#~StringReplace~{"U"->"i++,","D"->"i=i~Max~1-1,","R"->"i=0,"}<>"0}"],#2-1]&

0

Clojure,92 84字节

n到零而不是零n,可以利用take-while pos?

#(count(take-while pos?(reductions(fn[p o](if o(min(o p 1)%)%))%(map{\U -\D +}%2))))

原版的:

#(count(take-while(partial > %)(reductions(fn[p o](if o(max(o p 1)0)0))0(map{\U +\D -}%2))))

地图第二个参数U+D-和其他人nil。归约函数(operand position 1)以非null operand0其他方式运行。取值直到我们高于第一个输入参数,然后计算我们得到的数量。


0

Mathematica,67个字节

(p=i=0;While[p<#,p=Switch[#2[[++i]],"U",p+1,"D",1~Max~p-1,_,0]];i)&

两个参数(一个正整数和一个字符列表)的未命名函数,它们返回一个正整数。While比其他Mathematica条目更直接的实现,从而导致了更长的竞争时间。

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.