# 解决Dominion中的加倍和三倍动作

14

## 灵感

*从技术上讲，您选择受影响的行动是解决“王室”或“国王法院”的一部分，但这种观点对于解决这一挑战是比较干净的。

## Dominion中的两倍和三倍如何工作

``````2A -> AA
``````

``````A2BC -> ABBC
3DE -> DDDE
3N2BC3XY2 -> NNNBBCXXXY
``````

``````22AB -> AABB
``````

``````22A -> AA
``````

``````223BCDE -> BBBCCCDDE
``````

## 测试用例

``````(FY, FY)
(A2BC, ABBC)
(3DE, DDDE)
(3N2BC3XY2, NNNBBCXXXY)
(WA23G3GA, WAGGGGGGAAA)
(32, )
(2A32B2CDEFG, AABBCCDDEEFG)
(A2A323AB2CD2D2E3ABC, AAAAAABBBCCDDDDEEAAABBBC)
(P22LL3Q2Q22T, PLLLLQQQQQTT)
(322322ABCDEFGHIJKLMN, AABBCCDDEEEFFGGHHIJKLMN)
``````

5

## GolfScript（29 26字节）

``````](1/{\1+(3&@*.23-\1\$-@+}/;
``````

### 解剖

``````](         # Push an empty array under the input string to serve as rep stack
1/{        # Loop over the input string as a series of 1-char strings
#   Stack is ... reps ch
#   where the ... covers zero or more strings which will be output
\        #   Bring the rep stack to the top
1+(      #   Push a `1` on the bottom of it to avoid underflow and then pop
3&       #   Coerce to correct range, because if rep stack is a string then
#   we just got an ASCII value
@*       #   Apply repetition to the 1-char string: it's now an n-char string
.23-     #   Duplicate it and remove chars '2' and '3': this becomes output
\1\$-     #   Get the original copy and remove the output string's chars
#   So the stack is now ... reps output non-output
#   where non-output is either an empty string or a string of '2's
#   or '3's
@+       #   Push non-output onto the repetition stack
}/         # Loop
;          # Pop whatever's left of the repetition stack
``````

xnor

@xnor，这是内建参考。\交换堆栈中的前两个项目。

xnor 2014年

@xnor，不是每个堆栈项都是自己的堆栈；这是因为重复堆栈存储为数组或字符串（仍然是数组，但是某些内置函数对其进行了不同处理）。调试演示，该演示在主循环结束之前打印GS堆栈的内容。

4

# Javascript- 162152字节

``````F=I=>{L=c=>S.length;p=c=>L()?S.shift():d=>{};S=[(x=>/\d/.test(x)?(c,b)=>{for(c=p(),b=x;b--;)c();}:c=>s+=x)(m)for(m of I)];for(s='';L();)p()();return s;}
``````

``````F = I => {
L = c => S.length;
p = c => L() ? S.shift() : d => {};
S = [ (x => /\d/.test( x ) ?
(c,b) => {
for( c = p(), b = x; b--; )
c();
} : c =>
s += x
)(m) for( m of I ) ];

for( s = ''; L(); )
p()();

return s;
}
``````

### 样本输出

``````F('3N2BC3XY2')
"NNNBBCXXXY"

F('WA23G3GA')
"WAGGGGGGAAA"

F('A2A323AB2CD2D2E3ABC')
"AAAAAABBBCCDDDDEEAAABBBC"

F('322322ABCDEFGHIJKLMN')
"AABBCCDDEEEFFGGHHIJKLMN"

F('FY')
"FY"

F('')
""
``````

1

xnor

@xnor：我认为这很聪明。;）至于您的建议，您的直觉是正确的。我将这两种情况结合起来节省了10个字节。理想情况下是18，但我偶然发现了我认为是Firefox中的错误的信息。我应该能够`x`直接进行操作，而无需先将其复制到`b`范围为内部lambda 的变量中，但是Firefox无法正确评估循环条件。具体来说，`x`变为负数并且浏览器挂起。尝试替换`, b = x; b--;``; x--;`并运行输入`A2A323AB2CD2D2E3ABC`。如果有人读过这篇文章可以找出原因，...
COTO 2014年

...我很想知道。也许我缺少有关应该如何使用闭包的信息。
COTO 2014年

3

# C，115111字节

``````char*i,X[222],*s=X+99;main(){for(gets(i=X);*i;i++)*i<55?s=memset(s-*s,*i-49,*s+1):putchar(*i)**s?--*s,--i:++s;}
``````

``````#include <stdio.h>
#include <stdlib.h>
char I[99], S[99], *i = I, *s = S+66;
int n;
int main()
{
gets(I);
for(;*i;)
{
if(*i < '5') {
n = *s;
s[0] = s[1] = s[2] = *i - '1';
s += n;
i++;
} else {
putchar(*i);
if(*s)
--*s;
else
--s, ++i;
}
}
return 0;
}
``````

0

## 巨蟒（84）

``````S='1'*99
R=''
for c in input():q=int(S[0])*c;S=q*(c<'A')+S[1:];R+=q*(c>'3')
print(R)``````

`S`是乘数的堆栈（如果在前，则为顶）。已使用足够的进行了初始化，`1`以处理未乘的动作。