代码高尔夫宾果游戏!


14

给你一个 宾果游戏板和一个通话清单。您必须打印宾果!一旦您的董事会赢得比赛。

宾果板看起来像这样:

在此处输入图片说明

它们的指定方式如下:

14 29 38 52 74
4 18 33 46 62
7 16 * 60 71
9 27 44 51 67
12 23 35 47 73

董事会之后将立即发出呼叫,如下所示:

B7
I29
G60
G51
O71
I23
I16
N38

您必须将呼叫呼出到标准输出,直到赢得您的呼叫之后(获取满满的行,列或5长对角线),然后打印 BINGO!

对于上面的示例,打印:

B7
I29
G60
G51
O71
I23
I16
BINGO!

规则

标准代码高尔夫球规则,最短代码胜出。

细节

总会有足够的电话来保证您的宾果游戏。桌面上没有重复的号码,也没有重复的呼叫。棋盘将始终具有正确匹配的数字和字母(该B列仅包含1-15,该I列仅包含16-30,依此类推)。一个唯一的可用空间将始终位于中间,并标有*而不是数字。获胜的呼叫之后,可以使用和丢弃标准输入中的呼叫,但这不是必需的。

制作自己的测试用例!

Answers:


3

Perl,122 120个字符

$b=join'. .
',map~~<>,0..4;while(<>){/(\d+)/;$b=~s/\b$1\b/*/;print;
$b=~/(\*\s(\S+\s){$_}){4}\*/&&die"BINGO!
"for 0..7}

$b用两个附加的垃圾柱构建卡。将卡上被呼叫的号码替换为*并打印被呼叫的号码。然后,当*板上有5个规则间隔的s 时,最后一个正则表达式的值为true 。


4

C#– 536

(好的,这可能不是最合适的语言,但是无论如何……)

using System;using System.Collections.Generic;using System.Linq;class C{static void Main(){var s=Enumerable.Range(1,12).Select(_=>new HashSet<string>()).ToList();var b=Enumerable.Range(1,5).Select(_=>Console.ReadLine().Split(' ')).ToList();int i;for(i=0;i<5;++i){for(int j=0;j<5;++j){s[i].Add(b[i][j]);s[i+5].Add(b[j][i]);}s[10].Add(b[i][i]);s[11].Add(b[4-i][i]);}while(i>0){var l=Console.ReadLine();Console.WriteLine(l);l=l.Substring(1);foreach(var x in s){x.Remove("*");x.Remove(l);if(x.Count==0){Console.WriteLine("BINGO!");i=0;}}}}}

格式和评论:

using System;
using System.Collections.Generic;
using System.Linq;

class C
{
    static void Main()
    {
        // all possible winnable five-item sets – any one of them need to be emptied to win
        var s = Enumerable.Range(1, 12).Select(_ => new HashSet<string>()).ToList();
        // read the board from input to a list of arrays of numbers
        var b = Enumerable.Range(1, 5).Select(_ => Console.ReadLine().Split(' ')).ToList();
        int i;
        // split the board into the winnable sets
        for (i = 0; i < 5; ++i)
        {
            for (int j = 0; j < 5; ++j)
            {
                // sets 0–4 represent rows
                s[i].Add(b[i][j]);
                // sets 5–9 represent columns
                s[i + 5].Add(b[j][i]);
            }
            // set 10 represent one diagonal
            s[10].Add(b[i][i]);
            // set 11 represent the other diagonal
            s[11].Add(b[4 - i][i]);
        }
        while (i > 0)
        {
            // read and echo another input
            var l = Console.ReadLine();
            Console.WriteLine(l);
            // ignore the initial letter – we are guaranteed it is correct, anyway
            l = l.Substring(1);
            // remove the number from all sets
            foreach (var x in s)
            {
                x.Remove(l);
                // also remove the center * (inside the loop just to shave off a few characters)
                x.Remove("*");
                // if any set became empty, we won!
                if (x.Count == 0)
                {
                    Console.WriteLine("BINGO!");
                    // ensure the loop will stop (might not be necessary per the rules, but anyway)
                    i = 0;
                }
            }
        }
    }
}

4

Ruby 1.9(194,130)

这可能不是检查空列的最明智的方法,但这是我想尝试的第一件事!特别是,这#transpose花费很多。

声明木板时,木板与数字之间的空白行或固定宽度的字段都将节省很多字符。我想不出一种很好的方法来精确读取5行。

b=(R=0..4).map{gets}.join.scan /\d+|\*/
loop{gets
puts$_
~/\d+/
(e=b.index$&)&&b[e]=?*
R.map{|y|$><<:BINGO!&&exit if R.map{|x|[b[5*x+y],b[5*y+x],b[y<1?x*6:4*x+4]]}.transpose.any?{|a|a==[?*]*5}}}

编辑:从mob的perl答案中使用正则表达式技术的130个字符的解决方案:

b=(0..4).map{gets}*'~ ~ '
loop{gets
puts$_
~/\d+/
b[/\b#$&\b/]=?*
7.times{|i|$><<:BINGO!&&exit if b=~/(\*\s(\S+\s){#{i}}){4}\*/m}}

4

考虑到Rebol 即将公开发布开源软件的时间已经很久很久了,我回到了我的宠物方言中来解决这个Bingo问题。我也许很快就可以将Rebmu分发为自己的Teensy GPL软件包。:)


Rebmu 88个字符

在紧凑的表示法中:

rtZ5[GisGpcRaZisGaAPgPCaSB6zAPv'*]l5[AgL5[apGfAsk+A5]]hd+Gu[raGin-NTrM'*fisGv5]p"BINGO!"

该方言使用了一种我称之为糊涂的技巧,在Rebmu页面上有解释。从某种意义上讲它是“合法的”,因为它不会欺骗解析器。这是有效的Rebol ...并且实际上可以自由地与普通代码以及(长号)“长格式” Rebmu混合使用……BTW为141个字符:

[rt z 5 [g: is g pc r a z is g a ap g pc a sb 6 z ap v '*] l 5 [a: g l 5 [ap g f a sk+ a 5]] hd+ g u [ra g in- nt r m '* fis g v 5] p "BINGO!"]

(鉴于我认为压缩是一种无需自动化或编译就可以完成的技巧,实际上我以无格式形式开发代码。这并不困难。)

这实际上很简单,没什么特别的-我相信其他Rebol程序员可以省掉一些事情。一些 评论源在GitHub上,但是我使用的主要技巧是在很长的序列中构建所有可能的解决方案(“列表”,“数组”,“您拥有什么”)。我在输入循环中构建了对角线解,因为它在头部需要五个插入,在尾部需要五个追加才能完成……而且已经有一个五迭代循环。

整个过程很容易映射到Rebol代码,而且我还没有通过转置或其他经常出现的头将任何“矩阵库”扔到Rebmu中。有一天,我会这样做,但现在我只是在尝试相对接近Rebol本身的媒介。隐秘的事物,例如:

 [g: is g pc r a z is g a ap g pc a sb 6 z ap v '*]

...非常简单:

 [
     ; assign the series pointer "g" to the result of inserting 
     ; the z'th element picked out of reading in some series
     ; from input that was stored in "a"...this pokes an element
     ; for the forward diagonal near the front of g
     g: insert g (pick (readin-mu a) z)

     ; insert the read-in series "a" from above into "g" as well,
     ; but *after* the forward diagonal elements we've added...
     insert g a

     ; for the reverse diagonal, subtract z from 6 and pick that
     ; (one-based) element out of the input that was stored in "a"
     ; so an element for the reverse diagonal is at the tail
     append g (pick a (subtract 6 z))

     ; so long as we are counting to 5 anyway, go ahead and add an
     ; asterisk to a series we will use called "v" to search for
     ; a fulfilled solution later
     append v '*
 ]

注意:为清楚起见,在上面添加了括号。但是Rebol程序员(例如讲英语的人)通常避免使用额外的结构性标注来表示交流中的语法...而是将其保存用于其他应用...

就像额外的花钱说明这实际上有多有趣一样,我将混合一些普通代码来加总电路板。编程样式实际上是...兼容的:

rtZ5[GisGpcRaZisGaAPgPCaSB6zAPv'*]
temp-series: g
sum: 0
loop 5 * 5 [
    square: first temp-series
    if integer! == type? square [
        sum: sum + square
    ]
    temp-series: next temp-series
]
print ["Hey grandma, the board sum is" sum]
l5[AgL5[apGfAsk+A5]]hd+Gu[raGin-NTrM'*fisGv5]p"BINGO!"

这也是有效的Rebmu,并且在和您玩Bingo之前,它会给您带来一笔不错的董事会收入。在给出的示例中,它说Hey grandma, the board sum is 912。这可能是正确的。但是你明白了。:)


2

Mathematica 250

披露:我认为输入是在列表中给出的,对于Mathematica而言,使用列表自然就多了。因此,b代表董事会和c代表电话,

b//Grid
c//Column

输入

如果输入是字符串形式,则代码将增加约30个字符。(我将在以后包括该变化。)

y = ReplacePart[ConstantArray[0, {5, 5}], {3, 3} -> 1]; d = Diagonal;
t := Tr[BitAnd @@@ Join[y, Transpose@y, {d@y}, {d[Reverse /@ y]}]] > 0;
r@i_ :=(y = ReplacePart[y, Position[x, ToExpression@StringDrop[i, 1]][[1]] -> 1]; 
Print@If[t, Column[{i, "BINGO!"}], i])
n = 1; While[! t, r@c[[n]]; n++]

B7

I29

G60

G51

O71

I23

I16

答对了!


2

Python 249

R=raw_input;F=B=[[[x,0][x=='*']for x in row]for row in[R().split()for i in'11111']];A=any
while all([all(map(A,B)),all(map(A,zip(*B))),A(F[::6]),A(F[4:24:4])]):c=R();print c;C=c[1:];B=[[[x,0][x==C]for x in row]for row in B];F=sum(B,[])
print'BINGO!'

用法:

$ ./bingo.py < bingo.txt
B7
I29
G60
G51
O71
I23
I16
BINGO!

您可以替换row为一个字符的名称。未经测试:尝试i in'*'*5]替换[x=='*'][x==i]
恢复莫妮卡

2

杀伤人员地雷(82)

{(D≡(⍵∨⌽⍵)∧D←=/¨⍳⍴⍵)∨∨/(∧⌿⍵)∨∧/⍵:'BINGO!'⋄∇⍵∨B=⍎1↓⎕←⍞}0=B←↑{⍎(K,K)[⍞⍳⍨K←11↑⎕D]}¨⍳5
  • {... }¨⍳5:做5次:
  • ⍎(K,K)[⍞⍳⍨K←11↑⎕D]:读取一行(),然后将所有非数字或空格的字符映射到0,然后评估该行。
  • B←↑:将其转换为矩阵(如果输入正确,则为5x5),并存储在B中。
  • {... }0=B:起始板的可用空间(0)中为1,其他空间为0。
  • (D≡(⍵∨⌽⍵)∧D←=/¨⍳⍴⍵)∨∨/(∧⌿⍵)∨∧/⍵:如果填充了线,列或对角线:
  • 'BINGO!':然后输出 BINGO
  • ∇⍵∨B=⍎1↓⎕←⍞:否则,请读一行(),回显它(⎕←),放下第一个字符(1↓),对其求值以得到一个数字(),查看它在黑板上的位置(B=),标记它(⍵∨),然后重试()。 。

0

K,114

给董事会b和电话c

b{-1@y;a:(5*!5)_@[,/x;&(,/x)~\:1_y;:;"*"];$[max{max@min'"*"=,/'x}@/:(a;a ./:/:+:'(r;)'(r;|:r:!5));'"BINGO!";];a}/c

k)b
"14" "29" "38" "52" "74"
,"4" "18" "33" "46" "62"
,"7" "16" ,"*" "60" "71"
,"9" "27" "44" "51" "67"
"12" "23" "35" "47" "73"
k)c
"B7"
"I29"
"G60"
"G51"
"O71"
"I23"
"I16"
"N38"
k)b{-1@y;a:(5*!5)_@[,/x;&(,/x)~\:1_y;:;"*"];$[max{max@min'"*"=,/'x}@/:(a;a ./:/:+:'(r;)'(r;|:r:!5));'"BINGO!";];a}/c
B7
I29
G60
G51
O71
I23
I16
'BINGO
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.