67
\$\begingroup\$

In the card game Magic: the Gathering there are five different colours, which represent loose affiliations of cards, White (W), Blue (U), Black (B), Red (R) and Green (G). These are often arranged in a pentagon as follows:

  W
G   U
 R B

Both in the lore of MtG as well as in many card mechanics, adjacent colours in this pentagon are usually considered allies, and non-adjacent (sort of opposite) colours are considered enemies.

In this challenge, you'll be given two colours and should determine their relationship.

The Challenge

You're given any two distinct characters from the set BGRUW. You may take these as a two-character string, a string with a delimiter between the characters, two separate character values, two singleton strings, two integers representing their code points, or a list or set type containing two characters/strings/integers.

Your output should be one of two distinct and consistent values of your choice, one which indicates that the two colours are allies and one which indicates that they are enemies. One of those two values may be no output at all.

You may write a program or a function and use any of the our standard methods of receiving input and providing output.

You may use any programming language, but note that these loopholes are forbidden by default.

This is , so the shortest valid answer – measured in bytes – wins.

Test Cases

There are only 20 possible inputs, so I'll list them all.

Friends:

WU   UB   BR   RG   GW   UW   BU   RB   GR   WG

Foes:

WB   UR   BG   RW   GU   BW   RU   GB   WR   UG
\$\endgroup\$
  • 33
    \$\begingroup\$ Up next: implement the core rules :P \$\endgroup\$ – Captain Man Mar 14 '17 at 14:56
  • 12
    \$\begingroup\$ @CaptainMan i will upvote you if you can make it fit in a 30k character post :) \$\endgroup\$ – Walfrat Mar 14 '17 at 15:58
  • \$\begingroup\$ @Walfrat 30k? Should be possible \$\endgroup\$ – Not that Charles Mar 15 '17 at 21:03
  • 2
    \$\begingroup\$ @IvanKolmychek from the most unexpected alliances comes the most unexpected outcomes. \$\endgroup\$ – aluriak Mar 19 '17 at 1:59
  • 1
    \$\begingroup\$ Fun fact: Magic: The gathering is turing complete :) \$\endgroup\$ – Matthew Roh Mar 20 '17 at 8:04

47 Answers 47

82
\$\begingroup\$

JavaScript (ES6),  26 23 17 15  14 bytes

Takes input as two ASCII codes in currying syntax (a)(b). Returns 4 for friends or 0 for foes.

a=>b=>a*b/.6&4

Try it online!

How?

NB: only the integer quotient of the division by 0.6 is shown below.

Combo | a  | b  | a*b  | / 0.6 | AND 4
------+----+----+------+-------+------
  WU  | 87 | 85 | 7395 | 12325 |   4
  UB  | 85 | 66 | 5610 |  9350 |   4
  BR  | 66 | 82 | 5412 |  9020 |   4
  RG  | 82 | 71 | 5822 |  9703 |   4
  GW  | 71 | 87 | 6177 | 10295 |   4
  UW  | 85 | 87 | 7395 | 12325 |   4
  BU  | 66 | 85 | 5610 |  9350 |   4
  RB  | 82 | 66 | 5412 |  9020 |   4
  GR  | 71 | 82 | 5822 |  9703 |   4
  WG  | 87 | 71 | 6177 | 10295 |   4
------+----+----+------+-------+------
  WB  | 87 | 66 | 5742 |  9570 |   0
  UR  | 85 | 82 | 6970 | 11616 |   0
  BG  | 66 | 71 | 4686 |  7810 |   0
  RW  | 82 | 87 | 7134 | 11890 |   0
  GU  | 71 | 85 | 6035 | 10058 |   0
  BW  | 66 | 87 | 5742 |  9570 |   0
  RU  | 82 | 85 | 6970 | 11616 |   0
  GB  | 71 | 66 | 4686 |  7810 |   0
  WR  | 87 | 82 | 7134 | 11890 |   0
  UG  | 85 | 71 | 6035 | 10058 |   0

Previous approach, 15 bytes

Takes input as two ASCII codes in currying syntax (a)(b). Returns 0 for friends or 1 for foes.

a=>b=>a*b%103%2

Try it online!

How?

Combo | a  | b  | a*b  | MOD 103 | MOD 2
------+----+----+------+---------+------
  WU  | 87 | 85 | 7395 |    82   |   0
  UB  | 85 | 66 | 5610 |    48   |   0
  BR  | 66 | 82 | 5412 |    56   |   0
  RG  | 82 | 71 | 5822 |    54   |   0
  GW  | 71 | 87 | 6177 |   100   |   0
  UW  | 85 | 87 | 7395 |    82   |   0
  BU  | 66 | 85 | 5610 |    48   |   0
  RB  | 82 | 66 | 5412 |    56   |   0
  GR  | 71 | 82 | 5822 |    54   |   0
  WG  | 87 | 71 | 6177 |   100   |   0
------+----+----+------+---------+------
  WB  | 87 | 66 | 5742 |    77   |   1
  UR  | 85 | 82 | 6970 |    69   |   1
  BG  | 66 | 71 | 4686 |    51   |   1
  RW  | 82 | 87 | 7134 |    27   |   1
  GU  | 71 | 85 | 6035 |    61   |   1
  BW  | 66 | 87 | 5742 |    77   |   1
  RU  | 82 | 85 | 6970 |    69   |   1
  GB  | 71 | 66 | 4686 |    51   |   1
  WR  | 87 | 82 | 7134 |    27   |   1
  UG  | 85 | 71 | 6035 |    61   |   1

Initial approach, 23 bytes

Takes input as a 2-character string. Returns true for friends or false for foes.

s=>parseInt(s,35)%9%7<3

Try it online!

\$\endgroup\$
  • 10
    \$\begingroup\$ Ah, finally something fun. :) \$\endgroup\$ – Martin Ender Mar 13 '17 at 17:28
  • 4
    \$\begingroup\$ Fantastic find! \$\endgroup\$ – Greg Martin Mar 14 '17 at 23:48
  • \$\begingroup\$ Is there some clever maths I'm not aware of here or did you just brute force different modulos until you got one that worked? \$\endgroup\$ – FourOhFour Mar 19 '17 at 18:44
  • \$\begingroup\$ @FourOhFour It was brute forced. I think this is the smallest double modulo solution. But a port of this answer (which is using a comparison) would actually be one byte shorter. \$\endgroup\$ – Arnauld Mar 19 '17 at 19:24
  • 1
    \$\begingroup\$ @OddDev I actually tested all bits, not just the least significant one. For instance, a*b%290&8 would work just as well (producing 0 for friends or 8 for foes). \$\endgroup\$ – Arnauld Mar 20 '17 at 10:49
37
\$\begingroup\$

Jelly, 6 bytes

ạg105Ị

Takes two code points as argument. Yields 1 for friends, 0 for foes.

Try it online!

Background

Let n and m be the code points of two input characters. By taking |n - m|, we need to concern ourselves only with all 2-combinations of characters. The following table shows all 2-combinations of characters the the corresponding absolute differences.

WU  2
UB 19
BR 16
RG 11
GW 16

WB 21
UR  3
BG  5
RW  5
GU 14

All foe combinations are divisible by 3, 5, or 7, but none of the friend combinations this, so friends are exactly those that are co-prime with 3 × 5 × 7 = 105.

How it works

ạg105Ị  Main link. Left argument: n (code point). Right argument: m (code point)

ạ       Yield the absolute difference of n and m.
 g105   Compute the GCD of the result and 105.
     Ị  Insignificant; return 1 if the GCD is 1, 0 if not.
\$\endgroup\$
  • \$\begingroup\$ Nicely spotted! Why is the absolute value necessary? (I tried it online and it didn't give the right answer; but mathematically it shouldn't matter.) \$\endgroup\$ – Greg Martin Mar 14 '17 at 23:49
  • \$\begingroup\$ @GregMartin It isn't necessary; signed difference would work just as well. Subtraction is _ is Jelly. Did you use something else? \$\endgroup\$ – Dennis Mar 15 '17 at 0:26
  • \$\begingroup\$ Ah I see, I wrongly read as just absolute value, not absolute difference. \$\endgroup\$ – Greg Martin Mar 15 '17 at 4:15
35
\$\begingroup\$

Python 2, 19 bytes

"WUBRGWGRBUW".count

Try it online!

An anonymous function: returns 1 for friends and 0 for foes.

\$\endgroup\$
21
\$\begingroup\$

Befunge-98, 13 12 bytes

~~-9%5%3%!.@

Try it online!

Prints 0 for friends and 1 for foes

This uses the difference between the ASCII values of the letters.

If we take the (((ASCII difference % 9) % 5) % 3), the values for the foes will be 0. Then, we not the value and print it.

Thanks to @Martin for the golf

\$\endgroup\$
  • \$\begingroup\$ Use Jelly for 9 bytes: IA%9%5%3¬ Edit Try It Online! \$\endgroup\$ – Jonathan Allan Mar 13 '17 at 18:06
  • \$\begingroup\$ @JonathanAllan I see you already did! Nice. \$\endgroup\$ – MildlyMilquetoast Mar 13 '17 at 18:58
  • \$\begingroup\$ I actually morphed your method (using the actual, rather than absolute, difference) mod 9 mod 6, and used the fact that Jelly indexes into lists modularly to get it down to 7. I accredited you and linked here. \$\endgroup\$ – Jonathan Allan Mar 13 '17 at 19:00
  • \$\begingroup\$ @JonathanAllan I came up with the mod 9 mod 6 method too, but Befunge doesn't have actual difference or an absolute value, so it wasn't as feasible \$\endgroup\$ – MildlyMilquetoast Mar 13 '17 at 19:02
18
\$\begingroup\$

Jelly, 8 7 bytes

Piggybacks off of Mistah Figgins's fabulous Befunge answer!

Iị390B¤

Try it online!

How?

As Mistah Figgins noted the decision may be made by taking the absolute difference between the ASCII values mod 9 mod 5 mod 3 - 0s are then friends and 1s and 2s are enemies.

If we instead take the (plain) difference mod 9 we find that friends are 1s, 2s, 7s, and 8s while enemies are 3s, 4s, 5s, and 6s.

The code takes the difference with I and then indexes into the length 9 list [1,1,0,0,0,0,1,1,0], which is 390 in binary, 390B. The indexing is both modular (so effectively the indexing performs the mod 9 for free) and 1-based (hence the 1 on the very left).

\$\endgroup\$
16
\$\begingroup\$

C++ template metaprogramming, 85 bytes

template<int A,int B,int=(A-B)%9%5%3>struct f;template<int A,int B>struct f<A,B,0>{};

less golfed:

template<int A, int B,int Unused=(((A-B)%9)%5)%3>
struct foe;
template<int A, int B>
struct foe<A,B,0>{};

As this is a metaprogramming language, a construct compiling or not is one possible output.

An instance of f<'W','B'> compiles if and only if 'W' and 'B' are foes.

Math based off Befunge answer.

Live example.

As C++ template metaprogramming is one of the worst golfing languages, anyone who is worse than this should feel shame. ;)

\$\endgroup\$
  • \$\begingroup\$ Seems like there are a total of two redundant whitespaces inside templates. \$\endgroup\$ – Yytsi Mar 13 '17 at 19:04
  • \$\begingroup\$ @TuukkaX fixed, d'oh \$\endgroup\$ – Yakk Mar 13 '17 at 19:24
14
\$\begingroup\$

Ruby, 22 19 bytes

->x,y{390[(x-y)%9]}

Input: ASCII code of the 2 characters. Output: 1 for allies, 0 for enemies.

How it works:

Get the difference between the 2 numbers modulo 9, use a bitmask (390 is binary 110000110) and get a single bit using the [] operator.

\$\endgroup\$
  • 2
    \$\begingroup\$ Ah nice, I keep forgetting that integers can be indexed. +1 \$\endgroup\$ – Martin Ender Mar 13 '17 at 22:51
  • \$\begingroup\$ 16 bytes : ->x,y{x*y%103%2} Note that 0 and 1 are reversed. \$\endgroup\$ – Eric Duminil Mar 15 '17 at 14:04
  • 1
    \$\begingroup\$ And 15 bytes with x*y%51>9 like everybody else. I think it would be unfair to the upvotes to change it so radically now. \$\endgroup\$ – G B Mar 15 '17 at 14:17
10
\$\begingroup\$

CJam, 8 bytes

{*51%9>}

An unnamed block that expects two character codes on top of the stack and replaces them with 0 (friends) or 1 (foes).

Try it online!

Explanation

Well, we've seen a lot of fun arithmetic solutions now, so I guess it's fine if I present my own one now. The closest to this I've seen so far is Steadybox's C solution. This one was found with the help of a GolfScript brute forcer I wrote some time ago for anarchy golf.

Here is what this one does to the various inputs (ignoring the order, because the initial multiplication is commutative):

xy   x    y    x*y   %51  >9

WU   87   85   7395    0   0
UB   85   66   5610    0   0
BR   66   82   5412    6   0
RG   82   71   5822    8   0
GW   71   87   6177    6   0
WB   87   66   5742   30   1
UR   85   82   6970   34   1
BG   66   71   4686   45   1
RW   82   87   7134   45   1
GU   71   85   6035   17   1

We can see how taking the product of the inputs modulo 51 nicely separates the inputs into large and small results, and we can use any of the values in between to distinguish between the two cases.

\$\endgroup\$
9
\$\begingroup\$

Röda, 30 22 21 bytes

Bytes saved thanks to @fergusq by using _ to take the values on the stream as input

{[_ in"WUBRGWGRBUW"]}

Try it online!

The function is run like push "WU" | f after assigning a name to the function

Explanation

{                      /* Declares an anonymous function */
 [                 ]   /* Push */
  _ in                 /* the boolean value of the value on the stream is in */
      "WUBRGWGRBUW"    /* this string */
}
\$\endgroup\$
  • \$\begingroup\$ o_O lightning speed \$\endgroup\$ – Pavel Mar 13 '17 at 16:55
  • \$\begingroup\$ It's possible to save 5 bytes by reading the input values from the stream instead of taking parameters: {[(_.._)in"WUBRGWGRBUW"]}, but then the function must be called like [a, b] | f. \$\endgroup\$ – fergusq Mar 13 '17 at 17:01
9
\$\begingroup\$

05AB1E, 10 bytes

Returns 0 for friend and 1 for foe.

‘Û‹BWR‘ûIå

Try it online! or as a Test suite

Explanation

‘Û‹BWR‘     # push the string "RUGBWR"
       û    # palendromize (append the reverse minus the first char)
        Iå  # check if input is in this string
\$\endgroup\$
9
\$\begingroup\$

C, 33 32 29 24 22 bytes

#define f(k,l)k*l%51<9

Returns 1 if friends, 0 if foes.

\$\endgroup\$
8
\$\begingroup\$

Vim, 22 21 bytes

CWUBRGWGRBUW<esc>:g/<c-r>"/d<cr>

Input: a single line containing the two characters.

Output: empty buffer if friends, buffer containing WUBRGWGRBUW if enemies.

Explanation

C                                 # [C]hange line (deletes line into " register and enters insert mode)
 WUBRGWGRBUW<esc>                 # insert this text and exit insert mode
                 :g/      /d<cr>  # delete all lines containing...
                    <c-r>"        # ... the previously deleted input
\$\endgroup\$
  • 2
    \$\begingroup\$ You can do C instead of cw \$\endgroup\$ – Cows quack Mar 13 '17 at 17:37
8
\$\begingroup\$

Japt, 6 bytes

Inspired by @Martin Ender's solution.

Takes an array of two char codes as input.

×%51<9

Try it online! | Test Suite

Returns true for friends, false for foes.

14-byte solution:

Takes two char codes as input

nV a /3%3 f ¦1

Try it online! | Test Suite

Explanation:

nV a /3%3 f ¦1
nV a             // Absolute value of: First input (implicit) - Second input
      /3%3 f     // Divide by 3, mod 3, then floor the result
             ¦1  // Return true if the result does not equals 1, otherwise return false

12-byte solution:

"WUBRGW"ê èU

Try it online! | Test Suite

Explanation:

"WUBRGW"ê èU
"WUBRGW"ê     // "WUBRGW" mirrored = "WUBRGWGRBUW"
          èU  // Returns the number of times U (input) is found

Returns 1 for friends and 0 for foes.

9-byte solution:

Inspired by @Arnauld's solution.

*V%24%B%2

Test Suite

Returns 1 for friends, 0 for foes.

11-byte solution:

inspired by @Mistah Figgins's solution.

nV %9%5%3¦0

Test Suite

\$\endgroup\$
8
\$\begingroup\$

Brain-Flak, 155, 147, 135 bytes

(([(({}[{}]))<>])){({}())<>}(((([])[][][])[]())()()())<>{}<>{({}<><(({}))>)({}[{}]<(())>){((<{}{}>))}{}{{}({}<({}())>)(<()>)}{}<>}<>{}

Try it online!

This is 134 bytes of code plus one byte penalty for the -a flag which enables ASCII input.

This works by finding the absolute difference between the inputs, and checking if they equal 2, 11, 16, or 19. If it does, the input is a friend, and it prints a 1. If it's not, it prints nothing. Since nothing in brain-flak corresponds to an empty stack, which is falsy, no output is a falsy value. (meta)

One thing I particularly like about this answer, is that the "absolute difference" snippet (that is, (([(({}[{}]))<>])){({}())<>}{}{}<>{}) is not stack clean, but it can still be used in this answer since we don't care which stack we end up on before encoding the possible differences.

On a later edit, I took advantage of this even more by abusing the leftovers on the stack that doesn't end up with the absolute difference on it. On the first revision, I popped both of them off to keep it slightly more sane. Not doing this gives two major golfs:

  1. Obviously, it removes the code to pop them: {}{}, but more importantly:

  2. It allows us to compress the 2, 11, 16, 19 sequence from

    (((((()()))[][][](){})[][]())[])
    

    to

    (((([])[][][])[]())()()())
    

    Fortunately, there is no extra code needed to handle these leftovers later, so they are just left on the alternate stack.

Since brain-flak is notoriously difficult to understand, here is a readable/commented version:

#Push the absolute difference of the two input characters. It is unknown which stack the result will end on
(([(({}[{}]))<>])){({}())<>}

#Push 2, 11, 16, 19, while abusing the values left on the stack from our "Absolute value" calculation
(((([])[][][])[]())()()())

#Pop a zero from the other stack and toggle back
<>{}<>

#While True
{

    #Move top over and duplicate the other top
    ({}<><(({}))>)

    #Equals?
    ({}[{}]<(())>){((<{}{}>))}{}

    #If so:
    {

        #Increment the number under the stack
        {}({}<({}())>)
        #Push a zero
        (<()>)

    }

    #Pop the zero
    {}

    #Go back to the other stack
    <>

#Endwhile
}

#Toggle back
<>

#Pop a zero
{}
\$\endgroup\$
  • \$\begingroup\$ There's a push, pop you can remove, and you can push 0 in the if more efficiently to get down to 129: TIO \$\endgroup\$ – Riley Mar 13 '17 at 19:02
  • \$\begingroup\$ @Riley Cool, thanks for the tip! I like having a commented version, so I'm going to wait until I can comprehend that version before updating. \$\endgroup\$ – DJMcMayhem Mar 13 '17 at 21:52
  • \$\begingroup\$ It was only two minor changes. Here is the important part. My comments are in all caps, sorry if it looks like I'm yelling. \$\endgroup\$ – Riley Mar 13 '17 at 21:56
7
\$\begingroup\$

Jelly, 14 bytes

“WUBRG”wЀIAÆP

Returns 1 for enemies and 0 for friends.

Test suite at Try it online!

How?

“WUBRG”wЀIAÆP - Main link                                   e.g. WG
“WUBRG”        - ['W','U','B','R','G']
       wЀ     - first index of sublist mapped over the input     [1,5]
          I    - incremental differences                           -4
           A   - absolute value                                     4
            ÆP - is prime?                                          0
\$\endgroup\$
7
\$\begingroup\$

05AB1E, 7 bytes

$Æ105¿Ö

This is a port of my Jelly answer. Takes a list of code points as input. Prints 1 for friends, 0 for foes.

Try it online!

How it works

$        Push 1 and [n, m] (the input).
 Æ       Reduce [n, m] by subtraction, pushing n - m.
  105¿   Take the GCD of n - m and 105.
      Ö  Test if 1 is divisible by the GCD (true iff the GCD is ±1).
\$\endgroup\$
6
\$\begingroup\$

CJam, 16 12 11 10 bytes

Golfed 4 bytes by using Mistah Figgins's algorithm

Saved 1 byte thanks to Lynn

l:m9%5%3%!

Outputs 1 for enemy colours, 0 for ally colours.

Try it online! (Or verify all test cases)

Explanation

l           e# Push a line of input as a string
 :m         e# Reduce the string by subtraction (using the ASCII values)
   9%5%3%   e# Mod by 9, then by 5, then by 3. By doing this, enemy
            e#  pairs go to 0, and allies go to 1, 2, -1, or -2.
         !  e# Boolean negation
\$\endgroup\$
  • \$\begingroup\$ Don’t try to be too clever! l:m9%5%3%! is a byte shorter. \$\endgroup\$ – Lynn Mar 13 '17 at 19:11
  • \$\begingroup\$ @Lynn Oh wow, it is. That's kinda boring. Thanks \$\endgroup\$ – Business Cat Mar 13 '17 at 19:14
5
\$\begingroup\$

Retina, 18 bytes

O`.
BR|BU|GR|GW|UW

Try it online!

Quite straight-forward: sorts the input and tries to match any of the sorted ally pairs against it. Unfortunately, I don't think that Retina's string-based nature allows for any of the more interesting approaches to be competitive.

As a sneak peek for the next Retina version, I'm planning to add an option which swaps regex and target string (so the current string will be used as the regex and you give it a string to check), in which case this shorter solution will work (or something along those lines):

?`WUBRGWGRBUW
\$\endgroup\$
5
\$\begingroup\$

Java (OpenJDK 8), 28 23 bytes

-5 bytes thanks to fergusq

"WUBRGWGRBUW"::contains

Try it online!

\$\endgroup\$
  • 3
    \$\begingroup\$ Can you do "WUBRGWGRBUW"::contains? \$\endgroup\$ – fergusq Mar 13 '17 at 17:17
4
\$\begingroup\$

Brachylog, 10 bytes

A straightforward solution, no tricks involved.

p~s"WUBRGW

Try it online!

Explanation

p               A permutation of the input
 ~s             is a substring of
   "WUBRGW      this string
\$\endgroup\$
4
\$\begingroup\$

Jelly, 6 bytes

ạ:3%3Ḃ

For completeness's sake. Takes two code points as argument. Yields 0 for friends, 1 for foes.

Try it online!

Background

Let n and m be the code points of two input characters. By taking |n - m|, we need to concern ourselves only with all 2-combinations of characters. The following table shows all 2-combinations of characters the the corresponding absolute differences.

WU UB BR RG GW  WB UR BG RW GU
 2 19 16 11 16  21  3  5  5 14

If we divide these integers by 3, we get the following quotients.

WU UB BR RG GW  WB UR BG RW GU
 0  6  5  3  5   7  1  1  1  4

1, 4, and 7 can be mapped to 1 by taking the results modulo 3.

WU UB BR RG GW  WB UR BG RW GU
 0  0  2  0  2   1  1  1  1  1

Now we just have to look at the parity.

How it works

ạ:3%3Ḃ  Main link. Left argument: n (code point). Right argument: m (code point)

ạ       Absolute difference; yield |n - m|.
 :3     Integer division by 3, yielding |n - m| / 3.
   %3   Modulo 3, yielding |n - m| / 3 % 3.
     Ḃ  Parity bit; yield |n - m| / 3 % 3 & 1.
\$\endgroup\$
4
\$\begingroup\$

Cubix, 11 bytes

A Cubix implementation of Arnauld's solution.

U%O@A*'g%2W

Usage

Input the two characters, and it outputs 0 for friends and 1 for foes. Try it here.

Explanation

The code can be expanded like this.

    U %
    O @
A * ' g % 2 W .
. . . . . . . .
    . .
    . .

The characters are executed in this order (excluding control flow):

A*'g%2%O@
A         # Read all input as character codes
 *        # Multiply the last two character codes
    %     # Modulo the result by
  'g      #     103
      %   # And modulo that by
     2    #     2
       O  # Output the result ...
        @ # ... and terminate
\$\endgroup\$
3
\$\begingroup\$

Python 2, 26 bytes

lambda a:a in"GWUBRGRBUWG"

Try it online!

\$\endgroup\$
2
\$\begingroup\$

AWK, 23 Bytes

{$0="WUBRGWGRBUW"~$1}1

Example usage: awk '{$0="WUBRGWGRBUW"~$1}1' <<< UB

This prints 1 if the pair is a friend, 0 otherwise. I wanted to do something clever, but everything I thought of would be longer.

\$\endgroup\$
2
\$\begingroup\$

Jelly, 12 bytes

“WUBRGW”ŒBẇ@

Outputs 1 for allies, 0 for enemies.

Try it online!

Explanation

“WUBRGW”ŒBẇ@   Main link

“WUBRGW”       The string "WUBRGW"
        ŒB     Bounce; yields "WUBRGWGRBUW"
          ẇ@   Check if the input exists in that string
\$\endgroup\$
2
\$\begingroup\$

Ruby, 28 bytes

Outputs true for friend, false for foe:

p'WUBRGWGRBUW'.include?$**''

The ungolfed version isn't much different:

p 'WUBRGWGRBUW'.include?(ARGV.join(''))
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 7 bytes

Adaptation of the mod-trick from Jonathan's Jelly answer

Æ451bsè

Try it online! or as a Test suite

Explanation

 Æ        # reduced subtraction
  451b    # 451 to binary (111000011)
      sè  # index into this with the result of the reduced subtraction
\$\endgroup\$
2
\$\begingroup\$

GolfScript, 7 bytes

~*51%9>

Takes two code points as input.

Try it online! (Test suite which converts the input format for convenience.)

A GolfScript port of my CJam answer (which technically, is a CJam port of the result of my GolfScript brute forcer... uhhh...).

However, since GolfScript gets modulo with negative inputs right, there's a fun alternative solution at the same byte count which uses 4 for foes instead of 1:

~-)9%4&

Try it online!

xy   x    y    x-y    +1  %9  &4

WU   87   85     2     3   3   0
UB   85   66    19    20   2   0
BR   66   82   -16   -15   3   0
RG   82   71    11    12   3   0
GW   71   87   -16   -15   3   0
WB   87   66    21    22   4   4
UR   85   82     3     4   4   4
BG   66   71    -5    -4   5   4
RW   82   87    -5    -4   5   4
GU   71   85   -14   -13   5   4
\$\endgroup\$
2
\$\begingroup\$

Java 7, 38 bytes

int b(int a,int b){return(a-b)%9%5%3;}

Port from @Mistah Figgins' Befunge-98 answer is the shortest in Java 7 from the answers posted so far.
As for the others:

39 bytes: Port from @Arnauld's JavaScript (ES6) answer.

int a(int a,int b){return a*b%24%11%2;}

39 bytes: Port from @MartinEnder's CJam answer

Object e(int a,int b){return a*b%51>9;}

47 bytes: Port from @Steadybox' C answer

Object d(int a,int b){return(a=a*b%18)>7|a==3;}

52 bytes: Port from @Lynn's Python 2 answer

Object c(String s){return"WUBRGWGRBUW".contains(s);}

NOTE: Skipped answers which uses primes / palindromes and alike, because those are nowhere near short in Java. ;)
TODO: Coming up with my own answer.. Although I doubt it's shorter than most of these.

Try all here.


EDIT: Ok, came up with something myself that isn't too bad:

50 bytes:

Object c(int a,int b){return(a=a*b%18)>3&a<7|a<1;}

Explanation:

ab  a   b   a*b     %18

WU  87  85  7395    15
UB  85  66  5610    12
BR  66  82  5412    12
RG  82  71  5822    8
GW  71  87  6177    3
UW  85  87  7395    15
BU  66  85  5610    12
RB  82  66  5412    12
GR  71  82  5822    8
WG  87  71  6177    3

WB  87  66  5742    0
UR  85  82  6970    4
BG  66  71  4686    6
RW  82  87  7134    6
GU  71  85  6035    5
BW  66  87  5742    0
RU  82  85  6970    4
GB  71  66  4686    6
WR  87  82  7134    6
UG  85  71  6035    5

All enemies are either in the range 4-6 (inclusive) or 0.
EDIT2: Hmm.. I just noticed it's very similar to @Steadybox' answer.. :(

\$\endgroup\$
2
\$\begingroup\$

PHP, 31 bytes

echo!strstr(WBGURWRUGBW,$argn);

Run with echo AB | php -nR '<code>, where A and B are the two colors.

strtr returns the string from the position where the input is found;
with WBGURWRUGBW as haystack this returns a truthy string if the colors are foes; empty string if not.

! turns the truthy string to false, resulting in empty output
and the empty string to true, resulting in output 1.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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