玩消除游戏


12

介绍

在这个挑战中,您的任务是模拟某种消除游戏。在游戏中,参与者围成一个圆圈,每个人都拿着一个整数。在游戏的每一轮中,每个参与者都指向该人n走开(如果n他们持有的人数)。如果n为正,则在其右边进行计数;如果n为负,则在其左侧进行计数;如果n为零,则它们指向自己。每个有人指着他们的参与者都将被淘汰,并离开圈子。这结束了回合。巡回赛一直持续到没有参与者离开为止。

输入值

您的输入是非空整数列表,采用任何合理格式。它代表游戏参与者持有的数字。

输出量

您的输出是直到游戏结束为止的回合数。

考虑输入列表[3,1,-2,0,8]。在第一轮中,发生以下情况:

  • 持有人3指向持有人0
  • 持有人1指向持有人-2
  • 持有人-2指向持有人3
  • 持有人0指着自己。
  • 持有人8指向持有人的权利-2(列表代表一个圆圈,因此在末尾环绕)。

,这意味着0-23被淘汰,所以第二轮与清单进行[1,8]。在这里,1指向8,然后8指向自己,因此8被消除。第三轮与清单进行[1],在那里1只需要点在自理和消除。经过三轮淘汰所有参与者,所以正确的输出是3

规则和计分

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

测试用例

[3] -> 1
[0,0,0] -> 1
[-2,-1,0,1,2,3,4,5,6,7] -> 2
[5,5,5,6,6,6] -> 2
[3,-7,-13,18,-10,8] -> 2
[-7,5,1,-5,-13,-10,9] -> 2
[4,20,19,16,8,-9,-14,-2,17,7,2,-2,10,0,18,-5,-5,20] -> 3
[11,2,7,-6,-15,-8,15,-12,-2,-8,-17,6,-6,-5,0,-20,-2,11,1] -> 4
[2,-12,-11,7,-16,9,15,-10,7,3,-17,18,6,6,13,0,18,10,-7,-1] -> 3
[18,-18,-16,-2,-19,1,-9,-18,2,1,6,-15,12,3,-10,8,-3,7,-4,-11,5,-15,17,17,-20,11,-13,9,15] -> 6

您确定最后一个测试用例是5吗?
瑕疵的

@flawr我可以在大约一个小时内检查我的参考实现(必须离开我的电脑),但这应该是正确的。
Zgarb 2015年

只是要清楚一点:n此人持有的号码是多少?
彼得·泰勒

@PeterTaylor是的。我将在挑战的后面澄清这一点。
Zgarb 2015年

Answers:


4

Pyth,15个字节

f!=.DQ.e%+bklQQ

感谢kirby的测试套件

使用与@orlp相同的迭代机制,但是使用f“重复直到伪造”函数检测迭代次数,以检测[]完成后的迭代次数。


5

Matlab,91 77字节

function k=f(a);k=0;while a*0+1;l=numel(a);a(mod((1:l)+a-1,l)+1)=[];k=k+1;end

旧版:

function k=f(a);for k=1:numel(a);a(mod((1:l)+a-1,l)+1)=[];l=numel(a);if l==0;break;end;end

这是matlab的一个挑战,此代码的核心是删除数组条目:a(mod((1:l)+a-1,l)+1)=[]我认为这非常优雅。


4

CJam,21个字节

q~{__ee{~+0t}/0-}h],(

测试套件。

将输入作为CJam样式列表,但是测试套件负责从质询中转换格式。

说明

q~     e# Read and evaluate the input.
{      e# While the array is non-empty...
  _    e#   Copy the array. The original is left on the stack so that we can use the
       e#   stack depth to count the number of iterations later.
  _ee  e#   Make another copy and enumerate it, which means that each element is replaced
       e#   by a pair containing the element and its index in the array.
  {    e#   For each such pair...
    ~+ e#     Add the value to the index, giving the index it points at.
    0t e#     Set the value in the original array at the pointed-at index to 0.
       e#     This works without giving false positives because all 0s point to themselves.
  }/
  0-   e#   Remove all 0s from the array.
}h
],(    e# Wrap the stack in an array, get its length and decrement it to determine how
       e# many iterations this process took.

谢谢:ee几乎正是我昨天要寻找的另一个问题。
彼得·泰勒

3

C#,251个 219 211 197 193字节

最不可译的非神秘语言再次出现。

using System.Linq;class X{static void Main(string[]a){int i=0,c;for(;(c=a.Length)>0;i++)a=a.Where((e,x)=>!a.Where((f,y)=>((int.Parse(f)+y)%c+c)%c==x).Any()).ToArray();System.Console.Write(i);}}

该程序期望输入序列作为命令行参数。例如,要输入列表[5,5,5,6,6,6],请使用命令行参数调用它5 5 5 6 6 6

感谢MartinBüttner的一些提示。

通过意识到即使它是字符串数组,我也可以重用该数组,从而使它达到197args。我只需要将它们解析为一个整数即可。

通过意识到比短于193打高尔夫球。.Where(...==x).Any().Select(...).Contains(x)

不打高尔夫球

using System.Linq;
class X
{
    static void Main(string[] args)
    {
        var iterations = 0, count;

        // In the golfed version, this is a `for` loop instead.
        while ((count = args.Length) > 0)
        {
            // Create a new args array containing the items to be kept.
            args = args.Where((item, index) =>
            {
                // Should the item at index `index` be deleted?
                var deleteThisIndex = args.Where((item2, index2) =>
                    // Modulo that works with negative numbers...
                    ((int.Parse(item2) + index2) % count + count) % count
                        == index);

                return !deleteThisIndex.Any();

            }).ToArray();

            iterations++;
        }

        System.Console.Write(iterations);
    }
}

5
C#最难解决?当然,您一定会弄错。每个人都知道这是Java。:P
Alex A.

@AlexA。Pfft,我和Timwi在一起。我用Java击败了C#很多次:P
Geobits,2015年

3
您错了,Pyth或CJam是最难以理解的语言,C#是最不可消除的语言!
Beta Decay 2015年


2

R,105字节

l=scan();o=c();z=0;while((n=length(l))>0){for(i in 1:n)o=c(o,(i+l[i]-1)%%n+1);l=l[-o];o=c();z=z+1};cat(z)

不打高尔夫球

l <- scan()                  # get input as a 'l' vector from user
o <- c()                     # create a empty vector
z=0                          # create a counter starting at 0   
while((n=length(l))>0){      # while the length of the input is more than 0
  for(i in 1:n){             # iterate through the vector
    o=c(o,(i+l[i]-1)%%n+1)   # add the index that will be deleted to the 'o' vector
  }
  l=l[-o]                    # remove the 'o' vector indexes from 'l'
  o=c()                      # empty 'o'
  z=z+1                      # add 1 to counter
}
cat(z)                       # print the counter

2

Pyth,17个字节

tl.u.DN.e%+kblNNQ

巧合的是,与柯比范的答案非常相似。


2

Mathematica,71个字节

Length@FixedPointList[#~Delete~Mod[Plus~MapIndexed~#,Length@#,1]&,#]-2&

1
67:(i=0;#//.l:{__}:>l~Delete~Mod[++i;Plus~MapIndexed~l,Length@l,1];i)&
马丁·恩德

1
Plus~MapIndexed~#是真聪明,但我不知道如果没有使用较短的方式l+Range@Length@l
Martin Ender

1

STATA,146字节

inf a using a.
gl b=0
qui while _N>0{
g q$b=0
g c$b=mod(_n+a-1,_N)+1
forv y=1/`=_N'{
replace q$b=1 if _n==c$b[`y']
}
drop if q$b
gl b=$b+1
}
di $b

使用STATA的付费版本。假设输入在名为的换行符分隔文件中a.。限于由于允许的最大变量数而要求不超过1023回合的情况(可以固定为10个字节)。它读取数据并运行循环,直到没有更多观察结果为止。在每次迭代中,使用其指向的索引值创建一个变量。对于每个观察,如果另一个观察指向它,则设置一个指示器以删除变量。然后使用该指示符删除所有观察值并增加计数器。循环后,打印计数器。


1

Ruby,78 74字节

f=->a{b,i=[],0;(a.map{|e|b<<a[(e+i)%a.size]};a-=b;i+=1)while a.size>0;p i}

1

awk,66个字节

{for(;n=split($0=$0,a);++r)for(i in a)$((i+a[i]%n+n-1)%n+1)=X}$0=r

只需使用mod length array即可将其保留在数组中。在输入中,数字需要用空格分隔。

使用范例

echo "-2 -1 0 1 2 3 4 5 6 7" | awk '{for(;n=split($0=$0,a);++r)for(i in a)$((i+a[i]%n+n-1)%n+1)=X}$0=r'

这是适当格式的所有输入示例

3
0 0 0
-2 -1 0 1 2 3 4 5 6 7
5 5 5 6 6 6
3 -7 -13 18 -10 8
-7 5 1 -5 -13 -10 9
4 20 19 16 8 -9 -14 -2 17 7 2 -2 10 0 18 -5 -5 20
11 2 7 -6 -15 -8 15 -12 -2 -8 -17 6 -6 -5 0 -20 -2 11 1
2 -12 -11 7 -16 9 15 -10 7 3 -17 18 6 6 13 0 18 10 -7 -1
18 -18 -16 -2 -19 1 -9 -18 2 1 6 -15 12 3 -10 8 -3 7 -4 -11 5 -15 17 17 -20 11 -13 9 15

0

Python 2,122字节

def f(l):
 if not l:return 0
 L=len(l);x=[1]*L
 for i in range(L):x[(i+l[i])%L]=0
 return 1+e([v for i,v in zip(x,l)if i])
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.