The game Waving Hands is somewhere between a pen-and-paper Magic: the Gathering and a very elaborate Rock Paper Scissors. Each turn you write down one of eight gestures for each hand:

  • wiggled Fingers
  • proffered Palm
  • Snap
  • Wave
  • Digit pointing
  • Clap
  • staB
  • Nothing

There are few important rules that take precedence over the rest:

  • If both hands gesture P you automatically surrender. (This is a simplification, but we'll go with that.)
  • Only one hand at a time can gesture B.

A series of gestures performed by the same hand can cast a spell, where each spell is tied to one particular sequence. These sequences can overlap, but each gesture can only be used as the final gesture of a single spell (so you can create up to two spells in a single turn). Some spells require a gesture to be made with both hands simultaneously.

Your task is, given a list of gestures for each hand, to determine which spells could be cast in the current turn. For each hand, the last letter in the list represents the gesture chosen for the current turn.

Here is the list of gesture sequences. Upper case letters refer to a gesture performed by a single hand, lower case letters refer to a gesture performed with both hands.

B           Stab
cDPW        Dispel magic               
cSWWS       Summon elemental           
cw          Magic mirror               
DFFDD       Lightning bolt             
DFPW        Cure heavy wounds          
DFW         Cure light wounds          
DPP         Amnesia                    
DSF         Confusion                  
DSFFFc      Disease                    
DWFFd       Blindness                  
DWSSSP      Delayed effect             
DWWFWc      Raise dead                 
DWWFWD      Poison                     
FFF         Paralysis                  
FPSFW       Summon troll               
FSSDD       Fireball                   
P           Shield                     
p           Surrender                  
PDWP        Remove enchantment         
PPws        Invisibility               
PSDD        Charm monster              
PSDF        Charm person
PSFW        Summon ogre
PWPFSSSD    Finger of death
PWPWWc      Haste
SD          Missile
SFW         Summon goblin
SPF         Antispell
SPFPSDW     Permanency
SPPc        Time stop
SSFP        Resist cold
SWD         Fear
SWWc        Fire storm
WDDc        Lightning bolt
WFP         Cause light wounds
WFPSFW      Summon giant
WPFD        Cause heavy wounds
WPP         Counterspell
WSSc        Ice storm
WWFP        Resist heat
WWP         Protection from evil
WWS         Counterspell

Yes, Lightning bolt and Counterspell have two different sequences each.

You may write a program or function, taking input in any convenient list or string format, via STDIN, command-line argument or function argument, as long as it's not pre-processed. You may output pairs of spells via return value or by printing to STDOUT. Again, you may use any convenient list or string format, as long as the output is unambiguous.

You may assume that the sequences for left and right hand are the same length, and that they are valid (in particular, B will never be performed by two hands at the same time). Note that the input will only contain upper case letters. Lower case letters are only used in the above list to indicate that both hands need to perform the same (upper-case) gesture.

For spells which end in a two-hand gesture you should just output a single spell. If one hand cannot perform a spell, this needs to be indicated by a string containing only a hyphen, -.

This is code golf, so the shortest answer (in bytes) wins.


Each example consists of three lines: the left hand sequence, the right hand sequence (both input; the right-most gesture is the most recent one), and the list of all possible spell combinations (the output).

[["Cause light wounds","Counterspell"],["Shield","Counterspell"]]



[["Cure light wounds","-"]]


[["Magic mirror"]]

[["Summon goblin","Shield"],["Summon ogre","Shield"],["Summon troll","Shield"],
 ["Summon giant","Shield"],["Summon goblin","Amnesia"],["Summon ogre","Amnesia"],
 ["Summon troll","Amnesia"],["Summon giant","Amnesia"]]


[["Counterspell","Counterspell"],["Counterspell","Summon elemental"],
 ["Summon elemental","Counterspell"],["Summon elemental","Summon elemental"]]

[["Haste"],["Time stop"]]
  • \$\begingroup\$ @TheBestOne "in particular, B will never be performed by two hands at the same time" \$\endgroup\$ Commented Dec 20, 2014 at 5:39

4 Answers 4


Python 2, 685

Defines a function f accepting two strings. Return value is the string Surrender if the player surrenders; otherwise, a set consisting of strings (which indicate two-handed spells), and length-2 tuples of strings (indicating separate spells for each hand).

Base64 encoded source:


The source is of the formexec "eu352909HE%#NTHQh".decode('zip') which executes the following code:

L=dict(B='Stab',cDPW='Dispel magic',cSWWS='Summon elemental',DFFDD='Lightning bolt',DFPW='Cure heavy wounds',DFW='Cure light wounds',DPP='Amnesia',DSF='Confusion',DWSSSP='Delayed effect',DWWFWD='Poison',FFF='Paralysis',FPSFW='Summon troll',FSSDD='Fireball',P='Shield',PDWP='Remove enchantment',PSDD='Charm monster',PSDF='Charm person',PSFW='Summon ogre',PWPFSSSD='Finger of death',SD='Missile',SFW='Summon goblin',SPF='Antispell',SPFPSDW='Permanency',SSFP='Resist cold',SWD='Fear',WFP='Cause light wounds',WFPSFW='Summon giant',WPFD='Cause heavy wounds',WPP='Counterspell',WWFP='Resist heat',WWP='Protection from evil',WWS='Counterspell')
G=lambda l,r,L:{L[s]for s in L if l.endswith(s.upper())and all(r[~i]==s[~i].upper()or'a'>s[~i]for i in range(len(s)))}
B=dict(cw='Magic mirror',DSFFFc='Disease',DWFFd='Blindness',DWWFWc='Raise dead',PPws='Invisibility',PWPWWc='Haste',SPPc='Time stop',SWWc='Fire storm',WDDc='Lightning bolt',WSSc='Ice storm')
def f(l,r):c=G(l,r,B)|G(r,l,B);return[c|{(x,y)for x in G(l,r,L)or d for y in G(r,l,L)or d}-{(d,d)*(len(c)>0)},'Surrender'][l[-1]==r[-1]=='P']
  • \$\begingroup\$ @FryAmTheEggman fixed. \$\endgroup\$
    – feersum
    Commented Dec 19, 2014 at 17:14
  • \$\begingroup\$ Aha, neat use of sets to fix that :) \$\endgroup\$ Commented Dec 19, 2014 at 19:22

CJam, 987 931 925 663 bytes

qN/:Q;"Üãã0Ш¬Áó:-/ð1$½Ï¶¶òu´dóá3äÑì?´ì>³3cïCà×M×âÀCÒû¬u?xO,ÆFv)pÙû!¼ eÖUÈRÙnÂo/µ¾H¼RÎÚç¡ñ>z"32f-222bCb" BcDPWSwFdps"f=S/"[zõs¢@>2¯f¥sy<éSGÆÄbÚØÜ}}¤ºÅd¸(EWfa¤éõbfXpÀS7éÛM¼å© Ñæ¶a«I³þ>ÙÜ¡/¦$lâ µãÆØ¢§&á¼óÀwÏU¤ôYðçQòÔR#pÓzP×°~ï]v«viÚY¶¨¯`|o(Nê@æGÏ]ÌÉþMNÒÀ2yÌgÊ]Ê4Ê|BÃKWãIowIQ£Í-,_¥÷¶ºK-;Kr ÕtVæWï÷¦?+ÜÌÜó˯çp2Mvmc!hàtc»¬}sIæTÌÂ&ë6º²>Nº¯¦F¤½{ÜOSµ¸Ñ'|`²P³íD¿Z.c¢ Ò¿ñÅ¢MRöDk?à§iõ;­¡k'çTÑñ"31f-225b26b97f+:c"j"Ser'z/{(eu\+}%]z_{{~SaQX={_QV=W):W=={_el+m*}{m*:s}?}/:s{S+2$S+#)1$"-":D?@@}%-2<}%1:V(:X(:W;\}2*m*{z_,1>{)__~=\Da&,!={a+}{;}?$W>}*~}%_&_,({[D_]a-}*_{{`D&,!},}:A~,{A}*_["Surrender"_]a:S&,{;S}*p

Note that this contain all ASCII characters of ASCII code 32 to 255 only. Some characters might be skipped by SE, so better to copy the code from the link above

This above code is a base converted version of the following 925 byte solution:

"B|Stab|cDPW|Dispel magic|cSWWS|Summon elemental|cw|Magic mirror|DFFDD|Lightning bolt|DFPW|Cure heavy wounds|DFW|Cure light wounds|DPP|Amnesia|DSF|Confusion|DSFFFc|Disease|DWFFd|Blindness|DWSSSP|Delayed effect|DWWFWc|Raise dead|DWWFWD|Poison|FFF|Paralysis|FPSFW|Summon troll|FSSDD|Fireball|P|Shield|p|""Surrender":R"|PDWP|Remove enchantment|PPws|Invisibility|PSDD|Charm monster|PSDF|Charm person|PSFW|Summon ogre|PWPFSSSD|Finger of death|PWPWWc|Haste|SD|Missile|SFW|Summon goblin|SPF|Antispell|SPFPSDW|Permanency|SPPc|Time stop|SSFP|Resist cold|SWD|Fear|SWWc|Fire storm|WDDc|Lightning bolt|WFP|Cause light wounds|WFPSFW|Summon giant|WPFD|Cause heavy wounds|WPP|Counterspell|WSSc|Ice storm|WWFP|Resist heat|WWP|Protection from evil|WWS|Counterspell"]s'|/2/_

Newlines are just for show. This can be golfed a lot, but at least I got it to pass all test cases -_- .

This took longer than expected.

UPDATE 1 - golfed the code part a bit. Still scope for more. The string part can also be golfed/base converted, but I will refrain from that now as all other answers are not doing that either.

UPDATE 2 - Done golfing the code, now time for compressing that string.

UPDATE 3 - Base converted the string to reduce the code size to 663 bytes (It can be reduced to 640 bytes, but then there are 0 to 31 ASCII code characters too, which are really hard to deal with and I will only reside to them when I get beaten again).

Try it online here


Java, 1565 1388

Call with java M %leftString %rightString. Outputs Surrender if player surrenders. Otherwise outputs string in this form bothHandSpell;oneHandSpell,oneHandSpell;.... Note, this is a one liner solution for a rather complicated problem. : )

class M{String[]a="B,cDPW,cSWWS,DFFDD,DFPW,DFW,DPP,DSF,DWSSSP,DWWFWD,FFF,FPSFW,FSSDD,P,PDWP,PSDD,PSDF,PSFW,PWPFSSSD,SD,SFW,SPF,SPFPSDW,SSFP,SWD,WFP,WFPSFW,WPFD,WPP,WWFP,WWP,WWS,-".split(","),b="Stab,Dispel Magic,Summon elemental,Lightning bolt,Cure heavy wounds,Cure light wounds,Amnesia,Confusion,Delayed effect,Poison,Paralysis,Summon troll,Fireball,Shield,Remove enchantment,Charm monster,Charm person,Summon ogre,Finger of death,Missile,Summon goblin,Antispell,Permanency,Resist cold,Fear,Cause light wounds,Summon Giant,Cause heavy wounds,Counterspell,Resist heat,Protection from evil,Counterspell,-".split(","),c="cw,DSFFFc,DWFFd,DWWFWc,PPws,PWPWWc,SPPc,SWWc,WDDc,WSSc".split(",");public static void main(String[]a){System.out.println(new M().s(a[0],a[1]));}int e,f,g,h,i,j,k,l,m,n;String s(String o,String p){if(m(o,p,"p")>0)return"Surrender";String q="";f=e=0;for(;f<10;f++)q+=(m(o,p,c[f])|m(p,o,c[f]))>0?"Magic mirror,Disease,Blindness,Raise dead,Invisibility,Haste,Time stop,Fire storm,Lightning Bolt,Ice storm".split(",")[f*(e=1)]+";":"";for(f=0;f<33-e;f++)for(g=f<1?1:0,h=0;g<32-h&m(o,p,a[f])>0;)q+=m(p,o,a[++g*(e=1)])>0?b[f]+","+b[g*(h=1)]+";":"";return q;}int m(String o,String p,String r){k=n=0;if(r=="-")n=1;if((i=o.length())<(j=r.length()))return n;for(;k<j;)n|=((o.charAt(l=i-j+k)|32)!=((m=r.charAt(k++))|32)|((m|32)==m&(p.charAt(l)|32)!=(m|32)))?2:0;return n==2?0:1;}}


class M{
    String[]a="B,cDPW,cSWWS,DFFDD,DFPW,DFW,DPP,DSF,DWSSSP,DWWFWD,FFF,FPSFW,FSSDD,P,PDWP,PSDD,PSDF,PSFW,PWPFSSSD,SD,SFW,SPF,SPFPSDW,SSFP,SWD,WFP,WFPSFW,WPFD,WPP,WWFP,WWP,WWS,-".split(","),b="Stab,Dispel Magic,Summon elemental,Lightning bolt,Cure heavy wounds,Cure light wounds,Amnesia,Confusion,Delayed effect,Poison,Paralysis,Summon troll,Fireball,Shield,Remove enchantment,Charm monster,Charm person,Summon ogre,Finger of death,Missile,Summon goblin,Antispell,Permanency,Resist cold,Fear,Cause light wounds,Summon Giant,Cause heavy wounds,Counterspell,Resist heat,Protection from evil,Counterspell,-".split(","),c="cw,DSFFFc,DWFFd,DWWFWc,PPws,PWPWWc,SPPc,SWWc,WDDc,WSSc".split(",");
    public static void main(String[]a){
        System.out.println(new M().s(a[0],a[1]));
    int e,f,g,h,i,j,k,l,m,n;
    String s(String o,String p){
        String q="";
            q+=(m(o,p,c[f])|m(p,o,c[f]))>0?"Magic mirror,Disease,Blindness,Raise dead,Invisibility,Haste,Time stop,Fire storm,Lightning Bolt,Ice storm".split(",")[f*(e=1)]+";":"";
        return q;
    int m(String o,String p,String r){
            return n;
        return n==2?0:1;

Edit: Used Peter's suggestion to create a String array with less code.

Any hints to improve this would be appreciated.


JavaScript (ES6), 1070 941

Base64 encoded:


You run it by doing eval(atob("Zj0obCxyK...")) in the Firefox console.

Golfed code (may include characters not supported by Stack Exchange):

êÞÖX Ýp¢éݳ@Å[@&ë"k@Ï?@¨û¬ô
eYÍ¢+(@ÖXUÐö«j\\¬Í]ºi¨[k¢Ytô[Ab­æÚ]I ÃÑ(bzWt?D®®·§uêô§D^ÞÕéÜ©íéíÐðÖ?B'¾+"n)b·-?4
«Y¨Ë^¯CÒ=ªæÖ«²ô= ÅÑ+¦õ¢
ÞÐô[Ab«Öõuæ­CÖ<TH=jË^ÐõYg42+,W´H=ºi¨X(nX§Ñ!VÐ   íÊ^]<]z¹éÜËDô[Dâíl¶tHóÜѬËur]Ñ$?A^j½X=·µ²Ú+DYÍm)àÕº%·E
ÍjëÖX Ýp¢éݳE?D®j'ÖÝô[@ºÇµæ¯Ë\\(ºwlÑcÅ@¨º{^®Ê^]<ýqíl¶æÑdsD^²+-Ö·Eý®^rبWë¢m^¾)tYcô
§µêì¥éeÑe`).replace(/1/g," ").split(0).every((v,x,a)=>d[x%2||a[x+1]]=v);k=(h,o,G)=>(n=d[s],q=h.match(s+"$","i"))&&[...s].every((c,i)=>!(w=c.match("[a-z]"))||h[j=i+h.search(q)]==o[j])&&(w?~~D.indexOf(n)?D:[]:G).push(n);for(s in d){k(l,r,L);k(r,l,R);if(D==d["p"])return D}R=R-1?y:R;L=L-1?y:L;return D-1||R!=L?L.some(s=>R.some(t=>!D.push([s,t])))||D:D}

Since the spell dictionary consists of letters only, we can treat it as a base64-encoded string and use numbers as separators and spaces. I "decoded" the base64-like string into a binary string, which is 3/4 the size of the original.

Mildly ungolfed:


// y - default array
// D,R,L - result arrays for Doublehanded, Right and Left
// d - dictionary mapping of spell combos to spell names

// split string on `0` and replace `1`s with spaces, to populate `d`
btoa(`<UGLY BINARY STRING>`).replace(/1/g," ").split(0).every((v,x,a)=>d[x%2||a[x+1]]=v);

// given a main hand, off hand, and result array to populate,
// decide if current combo `s` applies
    (n=d[s],q=h.match(s+"$","i")) // does `s` match the end of `h`, case insensitive?
        !(w=c.match("[a-z]")) ||     // is every letter of `s` not lowercase
        h[j=i+h.search(q)]==o[j]  // or (if lowercase), a match between the hands?
    (w?                    // if the last letter so `s` was lowercase
        ~~D.indexOf(n)?    //   if D doesn't already have this spell
                       D   //      add it to D
                      :    //   else
                       []  //      add it to a throwaway array
      :G).push(n);         // else, add it to this hand's result array

for(s in d){
    k(l,r,L);    // test this spell against each hand
    if(D==d["p"]) return D  // if the doublehanded result is "Surrender"
                            // return immediately

R=R-1?y:R;   // replace empty arrays with ["-"]
L=L-1?y:L;   // (`[]-1` is -1, which is truthy, while ["foo"]-1 is `NaN`, falsy)

return D-1||R!=L         // if D is empty, or R and L not both empty, 
                ?        //   add all pairwise R/L combos to D and return D
                :D      //    otherwise, just return D


One golf I particularly like is testing for empty arrays with array - 1.

  • An empty array will produce a truthy value: [] - 1 => "" - 1 => 0 - 1 => -1 (truthy)
  • A filled array will produce a falsey value: ["foo"] - 1 => "foo" - 1 => NaN (falsey)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.