首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >六角形网格上的运动

六角形网格上的运动
EN

Code Golf用户
提问于 2016-01-24 17:14:29
回答 3查看 878关注 0票数 15

给定一系列字符的输入,表示六边形网格上的运动,输出“指针”的最终坐标。

我们的六边形将像这样编号(想象一个矩形网格,每一个奇数列都稍微向下移动):

代码语言:javascript
复制
  _____         _____         _____         _____
 /     \       /     \       /     \       /     \
/ -3,-2 \_____/ -1,-2 \_____/  1,-2 \_____/  3,-2 \
\       /     \       /     \       /     \       /
 \_____/ -2,-1 \_____/  0,-1 \_____/  2,-1 \_____/
 /     \       /     \       /     \       /     \
/ -3,-1 \_____/ -1,-1 \_____/  1,-1 \_____/  3,-1 \
\       /     \       /     \       /     \       /
 \_____/ -2,0  \_____/  0,0  \_____/  2,0  \_____/
 /     \       /     \       /     \       /     \
/ -3,0  \_____/ -1,0  \_____/  1,0  \_____/  3,0  \
\       /     \       /     \       /     \       /
 \_____/ -2,1  \_____/  0,1  \_____/  2,1  \_____/
 /     \       /     \       /     \       /     \
/ -3,1  \_____/ -1,1  \_____/  1,1  \_____/  3,1  \
\       /     \       /     \       /     \       /
 \_____/       \_____/       \_____/       \_____/

指针从(0,0)开始。

您必须支持的说明如下:

  • q:向左移动
  • w:往上走
  • e:往上移动-右移
  • a:向左移动
  • s:向下移动
  • d:往下移动-右移
  • r:顺时针旋转网格
  • R:逆时针旋转网格

旋转命令旋转整个网格,同时保持指针位于相同的坐标。(为什么是qweasd?它们与QWERTY键盘上的方向很好地匹配。

为了帮助可视化这一点,假设指针从中间开始,下面是移动命令将做的事情:

代码语言:javascript
复制
         _____
        /     \
  _____/   w   \_____
 /     \       /     \
/   q   \_____/   e   \
\       /     \       /
 \_____/       \_____/
 /     \       /     \
/   a   \_____/   d   \
\       /     \       /
 \_____/   s   \_____/
       \       /
        \_____/

在顺时针旋转(r)之后,这些命令被重新映射到(假设它是旋转整个十六进制网格,但仍然保持"w“向上,等等,这相当于以下内容):

代码语言:javascript
复制
         _____
        /     \
  _____/   e   \_____
 /     \       /     \
/   w   \_____/   d   \
\       /     \       /
 \_____/       \_____/
 /     \       /     \
/   q   \_____/   s   \
\       /     \       /
 \_____/   a   \_____/
       \       /
        \_____/

类似地,在此之后旋转逆时针(R)将使网格恢复正常,而逆时针旋转再次将qwedsa“映射”到aqweds

输入必须给定为单个字符串,并且输出可以是由任何非数字字符(例如)连接的单个字符串。( 1 23,4)或整数数组。

由于这是密码-高尔夫,以字节为单位的最短代码将获胜。

测试用例:

代码语言:javascript
复制
In                         Out
---------------------------------
edeqaaaswwdqqs             -2, 0
dddddddddd                 10, 5
wswseaeadqdq               0, 0
<empty string>             0, 0
esaaqrweesrqrq             -1, 0
wrwrwrwrw                  -1, 0
RRssrrrs                   -1, -1
aRRRRwddrqrrqqq            -1, -4
rrrrrrrrrrrrRRRRRRrrrrrrq  -1, -1
rrRrRrrRrrrrRRrRrRR        0, 0
EN

回答 3

Code Golf用户

回答已采纳

发布于 2016-01-25 05:18:38

Pyth,81字节

代码语言:javascript
复制
J_K1=Y"qwedsa"A,ZZFNz=kxYN ?<kZ=Y?<x\rNZ.>Y1.<Y1A,+G@[JZ1KZJ)k+H@[J_2JK2K)k;,G/H2

输出是表示坐标的整数列表。

我的解决方案实际上很无聊;它只是在数组( qwedsa)中查找输入的字符,然后访问两个数组,它们表示坐标中的相应变化。例如,如果输入是w,那么我们得到1(因为它是数组中的第二个字符)。然后,我们将A[1]添加到x (其中A是针对不同输入的x更改的数组),将B[1]添加到y ( By的更改)。rR是通过旋转qwedsa数组来实现的。

我相信有人能更好地利用派斯。不过,我会继续尝试打高尔夫球的!

你可以试试这里

票数 2
EN

Code Golf用户

发布于 2019-09-13 11:58:16

05AB1E,60 字节数

代码语言:javascript
复制
.•F?äM•U2Å0IvXy'rQiÀUëy'RQiÁUëykÐ5α‚ßsD3%_s3›·+‚<+]Ć`DÉ-2÷+‚

在网上试试验证所有测试用例.

解释:

一般解释:

我们从字符串"qwedsa"和坐标[0,0]开始,对输入的字符进行循环。

如果是"r“或"R”,我们将该字符串分别向左或右旋转。

如果没有,我们将在这个字符串中得到基于0的索引,并按如下方式映射它:

代码语言:javascript
复制
q → 0 → [-1,  0]
w → 1 → [ 0, -1]
e → 2 → [ 1, -1]
d → 3 → [ 1,  0]
s → 4 → [ 0,  1]
a → 5 → [-1,  1]

我们需要将索引数字转换为以下xy坐标:

代码语言:javascript
复制
 x   indices     y   indices
-1 ← 0;5        -1 ← 1;2
 0 ← 1;4         0 ← 0;3
 1 ← 2;3         1 ← 4;5

我们通过如下转换索引k来实现这一点:

x = min(k, abs(k-5))-1
y = (0\equiv k\pmod3) + 2(k\gt3) - 1

在整个循环之后,我们必须基于y-coordinate修复x-coordinate的关闭集,我们这样做(x保持不变):

y=y+\lfloor\frac{x-x\pmod2}{2}\rfloor

代码说明:

代码语言:javascript
复制
.•F?äM•         # Push compressed string "qwedsa"
       U        # Pop and store it in variable `X`
2Å0             # Push list [0,0]
                # (many 3-byte alternatives for this: `00S`; `т¦S`; `0D‚`; `1¾‰`; etc.)
   Iv           # Loop over each character `y` of the input:
     X          #  Push string `X`
      y'rQi    '#  If `y` equals "r":
           À    #   Rotate string `X` once towards the left
            U   #   And pop and store it as new value for `X`
      ëy'RQi   '#  Else-if `y` equals "R":
            ÁU  #   Do the same, but rotate right instead
      ë         #  Else:
       yk       #   Get the 0-based index of `y` in the string `X`
         Ð      #   Triplicate this index
          5α    #   Take the absolute difference with 5
            ‚ß  #   Pair it with the original index, and pop and push the minimum
                #   (maps 0→[0,5]→0; 1→[1,4]→1; 2→[2,3]→2;
                #         3→[3,2]→2; 4→[4,1]→1; 5→[5,0]→0)
         sD     #   Swap to get the original index again, and duplicate it
           3%   #   Take modulo 3
             _  #   And check if it's equals to 0 (1 if truthy; 0 if falsey)
          s3›   #   Swap to take the index again, and check if it's larger than 
                #   (again, 1 if truthy; 0 if falsey)
             ·  #   Double this
          +     #   And add both checks together
                #   (maps 0→1+0→1; 1→0+0→0; 2→0+0→0;
                #         3→1+0→1; 4→0+2→2; 5→0+2→2)
         ‚      #   Pair both mapped values together
          <     #   Decrease both by 1, so it becomes: 0→-1; 1→0; 2→1
           +    #   And add it to the current coordinates
    ]           # After the loop with inner if-else statements:
     Ć          # Enclose the coordinate, appending its own head: [x,y] becomes [x,y,x]
      `         # Push all three values separated to the stack
       D        # Duplicate this x
        É       # Check if its odd (1 if truthy; 0 if falsey)
         -      # Subtract it from the duplicated x
          2÷    # Integer-divide it by 2
            +   # Add it to y
             ‚  # And pair it with the original x again
                # (after which the result is output implicitly)

看这个05AB1E我的尖端(部分)如何压缩字符串而不是字典的一部分?)来理解为什么.•F?äM•"qwedsa"

票数 0
EN

Code Golf用户

发布于 2016-01-24 19:04:02

Python3,227个字节

代码语言:javascript
复制
def G(s):
 q='qwedsa'
 d=[-1,0,1,1,0,-1,-1,-2,-1,1,2,1]
 X=Y=0
 for c in s:
  if c in q:
   n = q.find(c)
   X += d[n]
   Y += d[n+6]
  if c == 'r':
   q = q[1:]+q[0]
  if c == 'R':
   q = q[5]+q[0:5]
 print(X, int((Y-X%2)/2))
票数 -1
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/70017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档