二进制数字魔术


28

挑战很简单;输出以下六个2D整数数组:

[[ 1, 11, 21, 31, 41, 51],
 [ 3, 13, 23, 33, 43, 53],
 [ 5, 15, 25, 35, 45, 55],
 [ 7, 17, 27, 37, 47, 57],
 [ 9, 19, 29, 39, 49, 59]]

[[ 2, 11, 22, 31, 42, 51],
 [ 3, 14, 23, 34, 43, 54],
 [ 6, 15, 26, 35, 46, 55],
 [ 7, 18, 27, 38, 47, 58],
 [10, 19, 30, 39, 50, 59]]

[[ 4, 13, 22, 31, 44, 53],
 [ 5, 14, 23, 36, 45, 54],
 [ 6, 15, 28, 37, 46, 55],
 [ 7, 20, 29, 38, 47, 60],
 [12, 21, 30, 39, 52]]

[[ 8, 13, 26, 31, 44, 57],
 [ 9, 14, 27, 40, 45, 58],
 [10, 15, 28, 41, 46, 59],
 [11, 24, 29, 42, 47, 60],
 [12, 25, 30, 43, 56]]

[[16, 21, 26, 31, 52, 57],
 [17, 22, 27, 48, 53, 58],
 [18, 23, 28, 49, 54, 59],
 [19, 24, 29, 50, 55, 60],
 [20, 25, 30, 51, 56]]

[[32, 37, 42, 47, 52, 57],
 [33, 38, 43, 48, 53, 58],
 [34, 39, 44, 49, 54, 59],
 [35, 40, 45, 50, 55, 60],
 [36, 41, 46, 51, 56]]

这些2D整数数组是什么?这些是魔术中使用的包含这些数字的数字卡:

enter image description here

魔术使某人想出一个在[1,60]范围内的数字,并给执行魔术的人提供所有包含该数字的牌。然后,执行魔术技巧的人可以将给定卡片的左上角数字相加(所有2的幂),以得出该人正在考虑的数字。有关此工作原理的其他说明,请参见此处。

挑战规则:

  • 您可以以任何合理的格式输出六个2D整数数组。可以打印定界符;可以是包含六个2D整数数组的3D整数数组;可以是行的字符串列表;等等
  • 您可以使用范围[-60, -1]或字符中的负值来填充最后四张卡的右下角位置,'*'而不必将其遗漏以使2D整数数组成为矩形矩阵(不,您不能用0或来填充它们-类似于null/的整数undefined,但*因为实际的卡片中也使用了星号)。
  • 矩阵中数字的顺序是强制性的。虽然这不要紧,物理魔术,我看到这个挑战主要表现为 - 之一,因此订单的限制。
    矩阵本身在输出列表中的顺序可以是任意顺序,因为从左上角的卡片可以清楚地看出哪个矩阵是哪个。

一般规则:

  • 这是,因此最短答案以字节为单位。
    不要让代码高尔夫球语言阻止您发布使用非代码高尔夫球语言的答案。尝试针对“任何”编程语言提出尽可能短的答案。
  • 标准规则适用于具有默认I / O规则的答案,因此允许您使用STDIN / STDOUT,具有适当参数的函数/方法以及返回类型的完整程序。你的来电。
  • 默认漏洞是禁止的。
  • 如果可能的话,请添加一个带有测试代码的链接(即TIO)。
  • 另外,强烈建议为您的答案添加说明。

有关。(例如,它指的是相同的魔术,但对于从我认为的挑战中获取灵感并不是很有用。该挑战要求输出真/假值,即数字是否n出现在第一k张卡片上;我的挑战是KC挑战以输出六个矩阵。)
Kevin Cruijssen

1
@DigitalTrauma嗯,我不确定这是否真的是重复的,因为您的挑战是ascii-art(没有这样的标签,但确实如此),而这个挑战允许您以更宽松的格式输出数组(不只是四种基本相同的方式)。不过,由于我有锤子,我无法投票决定重新开放。
暴民埃里克

@EriktheOutgolfer Woops ..忘了我也有一把锤子>。>有时能够关闭/打开锤子真是令人讨厌。.虽然它已经有了2票,所以对于您和我的还有4票。但是,如果有人想再次关闭它,我不在乎。它们的确确实非常相似,尽管他的挑战确实是[ascii-art]严格的(MD5)输出规则的挑战,其中我的位置非常灵活(行/列被交换,而范围[1,60]不是[1,63];很小的差异,但是仍然)。
Kevin Cruijssen

看来您没有以“这是我的宝贵挑战!”的态度尝试VTRO!至少...:P
暴民埃里克

1
我也忘记了锤子。我仍然认为这已经足够投票给dup,尽管如果重新开放,我会尊重社区的智慧。
Digital Trauma

Answers:


6

MATL12 11字节

-1字节感谢大师自己:)

60:B"@fQ6eq

说明:

60:           % create a vector [1,2,3,...,60]
   B          % convert to binary matrix (each row corresponds to one number)
    "         % loop over the columns and execute following commands:
     @f       % "find" all the nonzero entries and list their indices
       Q      % increment everything
        6e    % reshape and pad with a zero at the end
          q   % decrement (reverts the increment and makes a -1 out of the zero
              % close loop (]) implicitly
              % display the entries implicitly

在线尝试!


8

Perl 6的63 46个字节

say grep(*+&2**$_,^61)[$_,*+5...*for ^5]for ^6

在线尝试!

输出为多行上的2D数组,如有必要,将每个数组的最后一个数组切掉。


7

Python 2,76个字节

r=range;print[[[i for i in r(61)if i&2**k][j::5]for j in r(5)]for k in r(6)]

在线尝试!

这里的方法是创建所有可能数字的列表,r(61)然后将其缩减为卡的数字列表i&2**k

然后,通过使用列表切片,将一维数字列表重新排列为正确的6x5卡尺寸[card nums][j::5]for j in r(5)

然后,仅对6张卡片重复此生成器for k in r(6)


虽然找不到少于76个字节的解决方案,但这里还有两个也是76个字节的解决方案:

r=range;print[[[i for i in r(61)if i&1<<k][j::5]for j in r(5)]for k in r(6)]

在线尝试!

下一个灵感来自乔纳森·艾伦Jonathan Allan)

k=32
while k:print[[i for i in range(61)if i&k][j::5]for j in range(5)];k/=2

在线尝试!

任何意见,不胜感激。


6

木炭,26字节

E⁶E⁵⪫E⁶§⁺§⪪Φ⁶¹&πX²ι⁵ν⟦*⟧λ 

在线尝试!链接是详细版本的代码。我尝试直接计算条目,但是在调整*右下角的之前已经是27个字节。输出每行之间用空格和卡之间的空白行连接的行。说明:

E⁶                          Loop over 6 cards
  E⁵                        Loop over 5 rows
     E⁶                     Loop over 6 columns
           Φ⁶¹              Filter over 0..60 where
               π            Current value
              &             Bitwise And
                 ²          Literal 2
                X           Raised to power
                  ι         Card index
          ⪪        ⁵        Split into groups of 5
         §          ν       Indexed by column
        ⁺                   Concatenated with
                      *     Literal string `*`
                     ⟦ ⟧    Wrapped in an array
       §                λ   Indexed by row
    ⪫                       Joined with spaces
                            Implicitly print

*在看到实际卡片上的星星后,我添加了关于娱乐的规则。想知道是否会有任何语言在使用它,但我很高兴看到至少有一种语言在使用它。:)好答案!
Kevin Cruijssen

1
@KevinCruijssen Charcoal没有转置运算符,而高尔夫最多的转置需要已知大小的矩形数组,因此我需要添加一些内容来构成大小,并且*至少要与其他任何内容一样短。
尼尔

我不认为这是26个字节...
Tvde1

与该站点上的许多esolangs一样,@ Tvde1木炭使用自定义代码页。该页面上的字符花费1个字节,而其他字符花费最多4个字节。
尼尔

6

05AB1E,16个字节

60L2вíƶ0ζε0K5ô®ζ

在线尝试!

说明

60L                 # push [1 ... 60]
   2в               # convert each to a list of binary digits
     í              # reverse each
      ƶ             # multiply each by its 1-based index
       0ζ           # transpose with 0 as filler
         ε          # apply to each list
          0K        # remove zeroes
            5ô      # split into groups of 5
              ®ζ    # zip using -1 as filler

05AB1E,17个字节

6F60ÝNoôāÈϘ5ô®ζ,

在线尝试!

说明

6F                  # for N in [0 ... 5] do
  60Ý               # push [0 ... 60]
     Noô            # split into groups of 2^N numbers
        āÈÏ         # keep every other group
           ˜        # flatten
            5ô      # split into groups of 5
              ®ζ    # transpose with -1 as filler
                ,   # print

5

稻壳,13个字节

ṠMöTC5Wnünḣ60

在线尝试!

说明

          ḣ60  Range [1..60]
        ü      Uniquify using equality predicate
         n     bitwise AND: [1,2,4,8,16,32]
 M             For each number x in this list,
Ṡ     W        take the indices of elements of [1..60]
       n       that have nonzero bitwise AND with x,
    C5         cut that list into chunks of length 5
  öT           and transpose it.


5

Japt,14个字节

6Æ60õ f&2pX)ó5

试试吧

6Æ              Create a range from 0 to 5 (inclusive) and map each X into
  60õ             Elements in the range [1..60]
      f             Where
       &2pX)          The number bitwise AND with X is not 0
  ó5              Split into 5 arrays, where each array contains every 5th element

-Q flag is just for formatting purposes

4

JavaScript(ES6), 90  88字节

_=>[1,2,4,8,16,32].map(n=>(g=i=>i<60?g(++i,i&n?m[y%5]=[...m[y++%5]||[],i]:0):m)(y=m=[]))

在线尝试!

已评论

_ =>                        // anonymous function taking no argument
  [1, 2, 4, 8, 16, 32]      // list of powers of 2, from 2**0 to 2**5
  .map(n =>                 // for each entry n in this list:
    ( g = i =>              //   g = recursive function taking a counter i
      i < 60 ?              //     if i is less than 60:
        g(                  //       recursive call:
          ++i,              //         increment i
          i & n ?           //         if a bitwise AND between i and n is non-zero:
            m[y % 5] =      //           update m[y % 5]:
            [ ...m[y++ % 5] //             prepend all previous values; increment y
              || [],        //             or prepend nothing if it was undefined so far
              i             //             append i
            ]               //           end of update
          :                 //         else:
            0               //           do nothing
        )                   //       end of recursive call
      :                     //     else:
        m                   //       return m[]
    )(y = m = [])           //   initial call to g with i = y = m = []
                            //   (i and y being coerced to 0)
  )                         // end of map()


4

C(gcc),95字节

i,j,k;f(int o[][5][6]){for(i=6;i;)for(o[--i][4][5]=j=k=-1;j<60;)++j&1<<i?o[i][++k%5][k/5]=j:0;}

在线尝试!

以o中的3D int数组形式返回矩阵。

最后4个矩阵的最后一个值为-1。

感谢Kevin Cruijssen,节省了2个字节。

感谢Arnauld,节省了7 8个字节。


您可以通过更改o[i][4][5]=-1;for(j=k=0;为保存2个字节,for(o[i][4][5]=-1,j=k=0;以便可以删除括号。顺便说一句,我+1。
Kevin Cruijssen


(请注意,我不确定100%是否允许传递已经分配了正确尺寸的3D阵列。但是我会让普通C高尔夫球手对此提供更好的见解。)
Arnauld

@Arnauld我正在考虑这一点,但决定反对。
Matej Mulej

最好省去#include表明它不使用它
仅限ASCII的

3

CJam(18字节)

6{61{2A#&},5/zp}fA

在线演示。这是一个输出到stdout的完整程序。

解剖

6{             }fA    # for A = 0 to 5
  61{2A#&},           #   filter [0,61) by whether bit 2^A is set
           5/z        #   break into chunks of 5 and transpose to get 5 lists
              p       #   print

3

果冻,13 个字节

60&ƇⱮs€5LÐṂZ€

一个尼拉度链接,它产生一个(6)个整数列表的列表。(它使用默认选项不输出*或为负数。)

在线尝试!

怎么样?

每个矩阵按列优先顺序包含最多至 60 与左上(最小)数字共享单个置位。

该程序首先使所有 60 可能的数字顺序列表 [1个60]与它们的索引号共享任何设置位。然后将其分成多个块5并仅保留长度最小的索引-索引只有一个置位的索引(因此也是其最小值)。最后,它将每个转置以将它们置于列优先顺序中。

60&ƇⱮs€5LÐṂZ€ - Link: no arguments
60            - set the left argument to 60
    Ɱ         - map across ([1..60]) with:  (i.e. [f(60,x) for x in [1..60]])
   Ƈ          -   filter keep if:  (N.B. 0 is falsey, while non-zeros are truthy)
  &           -     bitwise AND
      €       - for each:
     s 5      -   split into chunks of five
         ÐṂ   - keep those with minimal:
        L     -   length
           Z€ - transpose each

许多15s都没有实现“分成五等分时的最小长度”技巧:

5Ż2*Ɱ60&ƇⱮs€5Z€
6µ’2*60&Ƈ)s€5Z€
60&ƇⱮ`LÞḣ6s€5Z€

...并且在尝试寻找更短的时间时,我又获得了13个,而根本不需要花招:

60B€Uz0Ts5ZƊ€

3

Wolfram语言(Mathematica),88字节

Transpose@Partition[#~Append~-1,5]&/@Last@Reap[Sow[,NumberExpand[,2]]~Do~{,60},Except@0]

随意添加了一个TIO链接(基于@ J42161217的答案)。向我+1。
Kevin Cruijssen


@ Mr.Xcoder谢谢。我用这个~招多了一个地方,取代了可变kNull。抱歉,没有时间添加tio链接。
Bruno Le Floch

2

Wolfram语言(Mathematica),99字节

Transpose@Partition[#~FromDigits~2&/@Last@GatherBy[{0,1}~Tuples~6,#[[-k]]&],5]~Table~{k,6}/. 61->-1

在线尝试!


你可以节省一些字符:做Transpose@代替Transpose[...]; 在分区之前填充到30个条目;使用Table[...,{k,6}]以避免k=#
Bruno Le Floch

@Bruno Le Floch Table可以保存一个字节。您尝试过transpose @吗?因为如果您仔细观看,它是行不通的。我是afk,但稍后
再打

抱歉,Transpose@在您进入PadRight房间后才能工作Partition。另一种意见是,该问题似乎不允许""使用占位符;您可以通过替换它而-1不会丢失任何字节。
Bruno Le Floch


2

R,73字节

`!`=as.raw;lapply(0:5,function(i)matrix(c((a=1:60)[(!a&!2^i)>0],-1),5,6))

我不确定我是否满足订购要求,因为默认情况下R会按列填充矩阵,因此实际出现在卡上的顺序与R中分配矩阵的方式相同。

在线尝试!


输出看起来不错。而且,如果R像几乎所有其他语言一样按行前的列填充矩阵,而不是像其他所有语言一样按列前的行填充矩阵,这仅意味着它是一种很好的编程语言,可以应对这一挑战。:)
Kevin Cruijssen

2

T-SQL,(1,168 1,139字节)

我只是想知道我能做到。

优化版本

 WITH g AS(SELECT 1 AS n UNION ALL SELECT n+1 FROM g WHERE n+1<61),B as(SELECT cast(cast(n&32 as bit)as CHAR(1))+cast(cast(n&16 as bit)as CHAR(1))+cast(cast(n&8 as bit)as CHAR(1))+cast(cast(n&4 as bit)as CHAR(1))+cast(cast(n&2 as bit)as CHAR(1))+cast(cast(n&1 as bit)as CHAR(1))as b FROM g),P as(SELECT * from (values(1), (2), (4), (8), (16), (32)) as Q(p)),S as(select distinct p,p+(substring(b,6,1)*1)*(case when p=1 then 0 else 1 end)+(substring(b,5,1)*2)*(case when p=2 then 0 else 1 end)+(substring(b,4,1)*4)*(case when p=4 then 0 else 1 end)+(substring(b,3,1)*8)*(case when p=8 then 0 else 1 end)+(substring(b,2,1)*16)*(case when p=16 then 0 else 1 end)+(substring(b,1,1)*32)*(case when p=32 then 0 else 1 end)as e from P cross apply B),D as(select * from S where e>=p and e<61),R as(select p,(row_number()over(partition by p order by cast(e as int)))%5 as r,e from D),H as(select k.p,'['+stuff((select','+cast(l.e as varchar)from R l where l.p=k.p and l.r=k.r for xml path('')),1,1,'')+']'as s from R k group by k.p,k.r)select stuff((select','+cast(x.s as varchar)from H x where x.p=z.p for xml path('')),1,1,'')from H z group by z.p

在线演示

在线尝试!

详细版本-带注释的SQL注释

WITH gen -- numbers 1 to 60
AS (
    SELECT 1 AS num
    UNION ALL
    SELECT num+1 FROM gen WHERE num+1<=60
),
BINARIES -- string representations of binaries 000001 through 111111
as (
SELECT 
    +cast( cast(num & 32 as bit) as CHAR(1))
    +cast( cast(num & 16 as bit)  as CHAR(1))
    +cast( cast(num & 8 as bit)  as CHAR(1))
    +cast( cast(num & 4 as bit)  as CHAR(1))
    +cast( cast(num & 2 as bit)   as CHAR(1))
    +cast(cast(num & 1 as bit)  as CHAR(1)) as binry FROM gen
),
POWERS -- first 6 powers of 2
as (
SELECT * from (values(1), (2), (4), (8), (16), (32)) as Q(powr)
),
SETELEMENTS -- cross apply the six powers of 2 against the binaries
-- returns 2 cols. col 1 = the power of 2 in question.
-- col 2 is calculated as that power of 2 plus the sum of each power of 2 other than the current row's power value, 
-- but only where a given power of 2 is switched "on" in the binary string, 
-- ie. where the first digit in the string represents 32, the second represents 16 and so on. 
-- That is, if the binary is 100100 then the number will be 
-- the sum of (32 x 1) + (16 x 0) + (8 x 0) + (4 x 1) + (2 x 0) + (1 x 0) 
-- but if the current row's power is 32 or 4, then just that number (32 or 4) is excluded from the sum.
-- rows are distinct.
as (
select distinct powr,
powr+
 (substring(binry,6,1) * 1) * (case when powr = 1 then 0 else 1 end)
 +(substring(binry,5,1) * 2) * (case when powr = 2 then 0 else 1 end)
 +(substring(binry,4,1) * 4) * (case when powr = 4 then 0 else 1 end)
 +(substring(binry,3,1) * 8) * (case when powr = 8 then 0 else 1 end)
 +(substring(binry,2,1) * 16) * (case when powr = 16 then 0 else 1 end)
 +(substring(binry,1,1) * 32) * (case when powr = 32 then 0 else 1 end) as elt
from POWERS cross apply BINARIES
),
DISTINCTELEMENTS -- purge calculated numbers smaller than the power of 2 or greater than 60
as (
select * from SETELEMENTS where elt >= powr and elt < 61
)--,
,
ROWNUMBERED -- for each power, number the rows repeatedly from 0 through 5, then back to 0 through 5 again, etc
as (
select powr, (row_number() over (partition by powr order by cast(elt as int)))%5 as r, elt  from DISTINCTELEMENTS
),
GROUPEDSETS -- for each row number, within each power, aggregate the numbers as a comma-delimited list and wrap in square brackets - the inner arrays
as (
select r1.powr, '['+stuff((select ',' + cast(r2.elt as varchar) from ROWNUMBERED r2 where r2.powr = r1.powr and r2.r = r1.r for xml path('')),1,1,'')+']' as s
from ROWNUMBERED r1
group by r1.powr,r1.r
)
select -- now aggregate all the inner arrays per power
stuff((select ',' + cast(g2.s as varchar) from GROUPEDSETS g2 where g2.powr = g1.powr for xml path('')),1,1,'')
from GROUPEDSETS g1
group by g1.powr

瞧!

Note 1: Some of the logic pertains to rendering square brackets and commas.

Note 2: Newer versions of SQLServer have more compact approaches to creating comma-delimited lists. (This was created on SQL Server 2016.)

Note 3: Arrays for a given card are not sorted (which is ok per the spec). Numbers within an array are correctly sorted. In this case, each "card" of the question, renders its arrays on a separate row in the results.

Shorter to hard-code arrays?

Yes.

Byte me.


Jeez, wouldn't it be shorter to just hardcode the result?
Jo King

Haha. Neither as fun, nor extensible.
youcantryreachingme

I do not fully understand -- are you saying that your solution only works by chance or are you convinced you followed the specifications correctly?
Jonathan Frech

@JonathanFrech - I did not explicitly code for the ordering of the numbers, although there may be an implicit condition in the language resulting in a guaranteed order. They render in correct ascending order. Separately, after posting, I realised I had misunderstood how the data were to be presented (in striped arrays per card, rather than one single set per card) - so have yet to solve that problem. As such, the result currently renders the correct numbers, in ascending order, within each of the 6 expected sets - see the linked sql fiddle. Still to do: break the sets into 5 subsets each.
youcantryreachingme

I appreciate your effort but if your solution is not correct, please fix it or delete your post. We generally do not allow for invalid answers to linger.
Jonathan Frech




1

MATLAB, 155 bytes

cellfun(@disp,cellfun(@(x)x-repmat(62,5,6).*(x>60),cellfun(@(x)reshape(find(x,30),[5 6]),mat2cell(dec2bin(1:62)-48,62,ones(1,6)),'Uniform',0),'Uniform',0))

This could be shorter as multiple lines but I wanted to do it in one line of code.


1
Could you perhaps add a TIO link with test code, so I can verify the output?
Kevin Cruijssen

1

05AB1E, 14 bytes

žOε60LDNo&ĀÏ5ι

Try it online!


1
Why the žO instead of just 6L? I know you're not using them in your map, but I'm curious why you've used aeiouy to create a list with 6 values. xD Nice answer, btw!
Kevin Cruijssen

1
@KevinCruijssen No particular reason, I just thought it was funnier than 6L, , , , or 9!.
Grimmy

It certainly caught my eye, that's for sure. ;)
Kevin Cruijssen

@KevinCruijssen I just realized тœ, ₅œ, ₁œ, also work, those are pretty cool too (:
Grimmy

₆b would work as well ;)
Kevin Cruijssen
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.