让我们练习421的得分规则!


13

421是在法国和其他一些欧洲国家受欢迎的骰子游戏。它主要在酒吧和酒馆中播放,以确定谁将购买下一轮饮料。完整游戏通常分两轮进行,每个玩家都试图摆脱令牌,但这与此处无关。(法语的维基百科页面。)

游戏中有3个标准方块骰子。

任务

您的任务是通过应用此游戏的评分规则,对从最高到最低的3个骰子骰子[X,Y,Z]的非空列表进行排序。

基本计分

  • 4,2,1是可能的最高组合。根据规则,它可能会得到8、10或11分。因为我们是对卷进行排序而不是对点进行计数,所以确切的值无关紧要。
  • 三张A:1,1,1是第二高的组合,得分7分。
  • 两张A:X,1,1(其中X是2到6)得到X分。
  • 三种类型:X,X,X(其中X为2到6)得到X分。
  • 直道:X,X + 1,X + 2分的分数2分。
  • 所有其他卷得分为1分。

结算关系

每当两卷获得相同的分数时,以下规则适用:

  • 两王牌比三王牌更好。例如:5,1,1击败5,5,5
  • 三分之二的2,2,2比直的要好。例如:2,2,2击败4,5,6
  • 直线从最低到最高排序。例如:4,5,6击败2,3,4
  • 通过将骰子从最高到最低排序来解决所有其他掷骰。例如:6,5,2击败6,4,3。(因此,游戏中最低的组合是2,2,1。)

以下是从最高到最低顺序排列的56个不同的卷:

421 111 611 666 511 555 411 444 311 333 211 222 654 543 432 321
665 664 663 662 661 655 653 652 651 644 643 642 641 633 632 631
622 621 554 553 552 551 544 542 541 533 532 531 522 521 443 442
441 433 431 422 332 331 322 221

挑战规则

  • 您可以采用任何合理的格式进行滚动,例如列表[[3,2,1],[4,2,1]]列表,字符串["321","421"]列表,整数列表[321,421]等。但是,每个骰子必须清楚地标识为16的值。
  • 对于每一卷,您可以假定骰子从最低到最高或从最高到最低排序,只要它是一致的即可。请在您的答案中说明您期望的顺序(如果有)。
  • 以字节为单位的最短答案为胜!

测试用例

使用骰子从高到低排序的字符串列表:

输入项

[ "321", "654" ]
[ "222", "321", "211" ]
[ "333", "311", "331", "111" ]
[ "111", "222", "333", "444" ]
[ "321", "421", "521", "621" ]
[ "422", "221", "442", "421", "222" ]
[ "222", "111", "421", "211", "651", "652", "543" ]

预期产出

[ "654", "321" ]
[ "211", "222", "321" ]
[ "111", "311", "333", "331" ]
[ "111", "444", "333", "222" ]
[ "421", "321", "621", "521" ]
[ "421", "222", "442", "422", "221" ]
[ "421", "111", "211", "222", "543", "652", "651" ]

排序应该稳定吗?
暴民埃里克(Erik the Outgolfer)

@EriktheOutgolfer所有卷都不同,并且始终可以根据联系规则对两个卷进行排序。
Arnauld

将每个骰子都设为0-5(而不是1-6)(例如,012代替123)是否可以?
wastl

@wastl我要说不。格式灵活,但是骰子值必须在[1 ... 6]中。
Arnauld

你把骰子带到酒吧吗?
Beta Decay '18

Answers:


6

Python,93字节

lambda a:sorted(a,key=lambda x:(x!=421,x>111,-(x%100<12)*x-(x%111<1)*x*.9,(x%111==99)*-x,-x))

在线尝试!


是什么使它特定于Python 2?
所罗门·乌科

@SolomonUcko Nothing,但是我想它被标记为Python 2,因为它是由TIO 的Code Golf Submission模板自动生成的。
Xcoder先生18年

1
您可以使用111 = 37 * 3(并且在所有可能的三元组中只有xxx三元组可以被37整除)这一事实来节省2个字节
digEmAll

1
通过使用该sort方法进行适当的排序,您可能会丢失2个字节。
Jakob

4

视网膜0.8.2,67字节

O$#^`(421|111)|(\d)((11)|\2\2)|(654|543|432|321)|\d{3}
$1$2$#4$#5$&

在线尝试!链接包括测试用例。说明:数字排序键的生成如下:

421     42100421
111     11100111
611-211   610611-  210211
666-222   600666-  600222
654-321    01654-   01321
653-221    00653-   00221

@Arnauld对不起,我还没有意识到我会把这些情况弄混。幸运的是,我认为我已经能够修复相同的字节数。
尼尔

现在看起来确实不错。
Arnauld

4

果冻 37  34 字节

;Ø1,ẋ3$)Ẏṙ-;‘r’Ɗ€;œċ3$Ṇ63¦
6RṚÇiµÞ

单子链接接受骰子骰列表列表(每个骰子降序排列),产生排序后的骰子降序排列。

在线尝试!或查看测试套件 ...或尝试按字典顺序排序的所有卷

怎么样?

此方法建立一个从最高到最低*的所有滚动列表,[4,2,1]并用0(无效的输入滚动)代替,以便使用Jelly的first-index-of原子对每个滚动进行排名i

*该列表还包括重复和冗余条目,这些条目不会影响行为:

[[1,1,1],[6,1,1],[6,6,6],[5,1,1],[5,5,5],[4,1,1],[4,4,4],[3,1,1],[3,3,3],[2,1,1],[2,2,2],[1,1,1],[7,6,5],[6,5,4],[5,4,3],[4,3,2],[3,2,1],[2,1,0],[6,6,6],[6,6,5],[6,6,4],[6,6,3],[6,6,2],[6,6,1],[6,5,5],[6,5,4],[6,5,3],[6,5,2],[6,5,1],[6,4,4],[6,4,3],[6,4,2],[6,4,1],[6,3,3],[6,3,2],[6,3,1],[6,2,2],[6,2,1],[6,1,1],[5,5,5],[5,5,4],[5,5,3],[5,5,2],[5,5,1],[5,4,4],[5,4,3],[5,4,2],[5,4,1],[5,3,3],[5,3,2],[5,3,1],[5,2,2],[5,2,1],[5,1,1],[4,4,4],[4,4,3],[4,4,2],[4,4,1],[4,3,3],[4,3,2],[4,3,1],[4,2,2],0,[4,1,1],[3,3,3],[3,3,2],[3,3,1],[3,2,2],[3,2,1],[3,1,1],[2,2,2],[2,2,1],[2,1,1],[1,1,1]]

;Ø1,ẋ3$)Ẏṙ-;‘r’Ɗ€;œċ3$Ṇ63¦ - Link 1, build rolls: descending pips, P  e.g. [6,5,4,3,2,1]
       )                   - for each:       e.g. X=5
 Ø1                        -   literal [1,1]
;                          -   concatenate        [5,1,1]
      $                    -   last two links as a monad (f(X)):
     3                     -     literal 3
    ẋ                      -     repeat           [5,5,5]
   ,                       -   pair               [[5,1,1],[5,5,5]]
        Ẏ                  - tighten (to a list of rolls rather than pairs of rolls)
          -                - literal -1
         ṙ                 - rotate left by (make rightmost [1,1,1] the leftmost entry)
               Ɗ€          - for €ach: last three links as a monad (f(X)):
            ‘              -   increment  -> X+1
              ’            -   decrement  -> X-1
             r             -   range      -> [X+1,X,X-1]
                           -   ...note: [7,6,5] and [2,1,0] are made but are redundant
           ;               - concatenate
                     $     - last two links as a monad (f(P)):
                    3      -   literal 3
                  œċ       -   combinations with replacement -> [[6,6,6],[6,6,5],...,[6,6,1],[6,5,5],...,[1,1,1]]
                 ;         - concatenate
                         ¦ - sparse application...
                       63  - ...to indices: 63
                      Ṇ    - ...what: NOT   -> replaces the [4,2,1] entry with a 0

6RṚÇiµÞ - Main Link: list of rolls
     µÞ - sort by the monadic link:
6       -   six
 R      -   range -> [1,2,3,4,5,6]
  Ṛ     -   reverse -> [6,5,4,3,2,1]
   Ç    -   call the last link as a monad -> [[1,1,1],[6,1,1],[6,6,6],[5,1,1],...]
    i   -   first index of (e.g.: [1,1,1]->1 or [6,1,1]->2 or [4,2,1]->0)

3

R,73字节

(x=scan())[order(x!=421,x>111,-(x%%100<12)*x-(!x%%37)*x*.9,x%%37!=25,-x)]

在线尝试!

  • 完整程序从stdin中获取整数列表,然后以降序返回它们(即421 ... 221
  • 开始时受到@Lynn答案的部分启发,现在基本上是对它的移植……因此归功于@Lynn;)
  • 保存2个字节得到除法余数x % 37而不是111

说明:

对于每个数字,将计算5个键,并按层次结构对数组进行排序:

key1 = 0 if equal to 421, 1 otherwise
key2 = 0 if equal to 111, 1 otherwise
key3 = 0 
       - 1.0*x if number is x11 numbers (2 aces)
       - 0.9*x if number is xxx numbers (3 of a kind)
key4 = 0 if number is a straight, 1 otherwise
key5 = -x

Then the array is sorted by key1 first, then by key2 in case of tie and so on...

2

05AB1E76 48 45 字节

421X36LR©vy11«y3∍}®vy>yJ})D®3ãJsм)˜©IΣ®sk>

事实证明,这比预期的要长得多,但是至少易于实现。会看看我是否能找到更好的解决方案。现在手工制作列表,但仍然有些长。

在线尝试验证所有测试用例

说明:

421             # Push 421 to the stack
X3             # ('1' lengthened to size 3) Push 111 to the stack
6LR             # Take the range [6, 1]
   ©            # Save this range in the register so we can re-use it
   v    }       # Loop over this range:
    y11«        #  Merge '11' with the current number `y`, and push it to the stack
    y3         #  Lengthen the current number `y` to size 3, and push it to the stack
   ®v      }    # Load the range [6, 1] from the register, and loop over it again:
     y>y      #  Take the range [`y+1`, `y-1`]
          J     #  And join them together to single 3-digit numbers
)               # Push everything that's now on the stack to a single list
 D              # Duplicate this list
  ®             # Load the range [6, 1] from the register again
   3ã           # Take the cartesian repeated three times
     R          # Reverse this list
      J         # Join every inner list to a single 3-digit number
 s              # Swap, so the duplicate list of above is at the top of the stack again
  м             # And remove all those items from the cartesian list
   )            # Combine both lists (as list of lists)
    ˜           # Flatten it
     ©          # Store this full completed list (now equal to the challenge description,
                #  with some additional values we can ignore) in the register
I               # Take the input-list
 Σ              # Sort it by:
  ®             #  The list we generated above
   sk           #  Take the 0-indexed indices
     >          #  And increase it by 1 (because apparently 0 is placed at the back,
                #  so '421' would always be at the end)

这是代码第一部分生成的实际列表:

['421','111','611','666','511','555','411','444','311','333','211','222','111','111','765','654','543','432','321','210','','665','664','663','662','661','656','655','','653','652','651','646','645','644','643','642','641','636','635','634','633','632','631','626','625','624','623','622','621','616','615','614','613','612','','566','565','564','563','562','561','556','','554','553','552','551','546','545','544','','542','541','536','535','534','533','532','531','526','525','524','523','522','521','516','515','514','513','512','','466','465','464','463','462','461','456','455','454','453','452','451','446','445','','443','442','441','436','435','434','433','','431','426','425','424','423','422','','416','415','414','413','412','','366','365','364','363','362','361','356','355','354','353','352','351','346','345','344','343','342','341','336','335','334','','332','331','326','325','324','323','322','','316','315','314','313','312','','266','265','264','263','262','261','256','255','254','253','252','251','246','245','244','243','242','241','236','235','234','233','232','231','226','225','224','223','','221','216','215','214','213','212','','166','165','164','163','162','161','156','155','154','153','152','151','146','145','144','143','142','141','136','135','134','133','132','131','126','125','124','123','122','121','116','115','114','113','112','']

在线尝试。

如您所见,其中包含空项目;像765或的数字210; 和范围内的数字[216, 112]。但是我们都可以忽略这些,因为我们要实际排序的项目的顺序正确。


原始的76个字节的解决方案:

Σ•X˜„Éε´õñ€9Ú ù?ä09úd5àPÎøŒccuĆYÑ,o¾eíË¿¼À8@gID=vÆOΣxjF¨OJ₃^εR\èv667вsk>

在线尝试 验证所有测试用例(没有测试套件,因为仍然存在一个排序错误,压缩数字没有看到结束括号以停止排序)。

说明:

Σ             # Sort by:
 •...•667в    #  The correctly sorted list of all possible rolls from the challenge
 sk           #  Take the 0-indexed indices
   >          #  And increase it by 1 (because apparently 0 is placed at the back,
              #  so '421' would always be at the end)

说明 •X˜„Éε´õñ€9Ú ù?ä0₅9úd5àPÎøŒccuĆYÑ,o¾eíË¿¼À8@gID=vÆOΣxjF¨O‡J₃^εR™\èv•667в

两者之间的所有内容都是由以下程序生成压缩数字

Z 255B

在线尝试。

  • Z>:取列表的最大值+ 1(667在这种情况下)
  • β:将列表从Base- 667转换为单个数字
  • 255B:并将那个数字转换为基数255(使用05AB1E的代码页),因此我们有了压缩数字

通过使用667в它将压缩后的数字转换回原始列表。


2

JavaScript(ES7),96个字节

d=>d.sort((a,b,g=(n,[x,y,z]=n)=>n**=n-421?y*z-1?x-y|y-z?~y%x|~z%y?1:2:3:x-1?3.1:5:5)=>g(b)-g(a))

严格按照评分规则对卷进行排序。期望一个字符串数组,其中各个字符按值的降序排列["654"]

在线尝试!

说明

卷的类别提高到以下指数:

421                 5
Three Aces          5
Two Aces            3.1
Three-of-a-Kind     3
Straights           2
Other               1

不打高尔夫球

精神上解开条件检查会让我头疼,而且我敢肯定,可以通过某种方式进一步打高尔夫球。

var f =
d => d.sort(
    (
     a, b,
     g = (n, [x, y, z] = n) =>               // destructure rolls, e.g. "321" => ["3","2","1"]
        n **=                                // differentiate scores by raising to the exponent corresponding to its category in the table above
                                             // all conditionals check for a falsy value
            n - 421
                ? y * z - 1                  // ends with "11"
                    ? x - y | y - z          // three-of-a-kind
                        ? ~y % x | ~z % y    // straights, with operators chosen to correctly convert strings to numbers
                            ? 1
                            : 2
                        : 3
                    : x - 1                  // three aces
                        ? 3.1
                        : 5
                : 5
    ) =>
    g(b) - g(a)
)

1

Javascript,101个字符,116个字节(UTF-8)

a=>(r=d=>(i="ƥoɣʚǿȫƛƼķōÓÞʎȟưŁ".indexOf(String.fromCharCode(d)),i<0?700-d:i),a.sort((a,b)=>r(a)-r(b)))

采取一组数字表示卷,如[ 321, 654 ]

在线尝试!

说明:

我拿了56个可能的不同卷中的前16个(实际上并没有遵循任何顺序),并将它们编码为字符串:"ƥoɣʚǿȫƛƼķōÓÞʎȟưŁ"。该字符串的每个字符对应于前16个可能的滚动(ƥis 421ois 111,...)。现在,对于数组的每个两个元素ab我们只是从字符串中检查它们的索引,如果包含它们,则使用索引,否则(索引为-1),我们使用转数本身(我们将其减去700以与不包括在内,按降序排序。


注意:除非另有说明,否则代码长度始终以字节为单位。字符数有时可能是相关的,但我不认为这是这里。相关元答案
Arnauld

@Arnauld是的!顺便说一句,我是问那个元问题的人;-)
易卜拉欣·马里尔

1
天哪!我完全错过了这个细节...:D
Arnauld '18

1

干净169个 102字节

所有八进制转义都计为一个字节,因为Clean编译器会很乐意采用这种方式,但是TIO和SE不会,因为它们的格式不正确。

import StdEnv
$l=[e\\c<-:"\245\157\143\232\377\53\233\274\67\115\323\336\216\37\260\101\231\230\227\226\225\217\215\214\213\204\203\202\201\171\170\167\156\155\52\51\50\47\40\36\35\25\24\23\n\11\273\272\271\261\257\246\114\113\102\335",e<-l|c==toChar e]

在线尝试!

使用所有骰子掷骰(以256模为整数的整数)的事实。
方便地,Char(通常)被视为256模整数。


1

Pyth,48个字节

oxs[]j421T]j7 2_sm+Ld,,ddjT9tS6_m_+LdS3 4.C_S6 3

期望输入为嵌套数组,每个元素按降序排列。在此处在线尝试,或在此处一次验证所有测试用例。

oxs[]j421T]j7 2_sm+Ld,,ddjT9tS6_m_+LdS3 4.C_S6 3)NQ   Final 3 tokens inferred from context
                                                      Implicit: Q=eval(input()), T=10
    ]j421T                                            Convert 421 to base 10, wrap in array -> [[4,2,1]]
          ]j7 2                                       Convert 7 to base 2, wrap in array -> [[1,1,1]]
                 m          tS6                       Map d in [2,3,4,5,6] using:
                      ,dd                               [d,d]
                         jT9                            Convert 10 to base 9 -> [1,1]
                  +Ld,                                  Prepend d to each of the above
               _s                                     Flatten and reverse -> [[6,1,1],[6,6,6]...[2,2,2]]
                                m       4             Map d in [0,1,2,3] using:
                                  +LdS3                 [d+1,d+2,d+3]
                                 _                      Reverse the above
                               _                      Reverse the result -> [[6,5,4]...[3,2,1]]
                                           _S6        [6,5,4,3,2,1]
                                         .C    3      All 3-element combinations of the above, respecting order
  s[                                            )     Wrap the 5 previous sections in an array, concatenate
ox                                               NQ   Order Q using each element's index in the above list

1

05AB1E,24个字节

Σ5βËyθyP‚yнÃ6βyË_y¥PΘy)(

在线尝试!

总体算法:

Σ          )( # sort by the following, in decreasing priority:
 5βË          # 1 for 421, 0 otherwise
 yθyP‚yнÃ6β   # 7 for 111, X for XXX and X11, 0 otherwise
 yË_          # 0 for XXX, 1 otherwise
 y¥PΘ         # 1 for sequences, 0 otherwise
 y            # the roll itself

细节:

5β          # parse the roll as a base-5 number
  Ë         # are all digits equal? (base-5 421 is 111)

yθ          # last die (equal to the 1st die iff we have XXX)
  yP        # product of dice (equal to the 1st die iff we have X11)
    ‚       # wrap the two in an array
     yнà    # keep only those that are equal to the 1st die
        6β  # parse as a base-6 number (111 -> [1, 1] -> 7)

yË          # are all dice equal?
  _         # boolean negation

y¥          # deltas ([a, b, c] -> [b - a, c - b])
  P         # product
   Θ        # 1 if it's equal to 1, 0 otherwise
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.