9
\$\begingroup\$

Objective

Given a list of three Wuxings, compare them in rock-paper-scissors fashion, and output which Wuxing is the winner.

Wuxings

Wuxings(五行) are the five "elements" in Chinese philosophy. They are Fire(火), Water(水), Wood(木), Metal(金), and Soil(土).

Positive actions

A positively acted Wuxing gains 1 point.

  • Fire fertilizes Soil.
  • Soil incubates Metal.
  • Metal is a container for Water.
  • Water nourishes Wood.
  • Wood fuels Fire.

Negative actions

A negatively acted Wuxing loses 1 point.

  • Fire melts Metal.
  • Metal chops Wood.
  • Wood penetrates Soil.
  • Soil blocks Water.
  • Water extinguishes Fire.

I/O format

You can encode the Wuxings freely, but you must represent each Wuxing consistently in the input and the output.

The inputted list is not assumed to be sorted.

Otherwise Flexible.

Examples

Input, Score, Winner

[Fire, Water, Wood], [0, 0, +1], Wood
[Fire, Water, Metal], [-1, +1, -1], Water
[Fire, Fire, Water], [-1, -1, 0], Water
[Fire, Water, Water], [-2, 0, 0], Water
[Fire, Fire, Wood], [+1, +1, 0], Fire
[Fire, Wood, Wood], [+2, 0, 0], Fire
[Fire, Fire, Fire], [0, 0, 0], Fire

Note that this exhausts all possibilities up to symmetry, and every case admits a unique winner.

\$\endgroup\$
2
  • \$\begingroup\$ Just to be clear, we output only the winner? Or also the scores of each participant? \$\endgroup\$ Commented Nov 7 at 8:54
  • \$\begingroup\$ @mousetail'he-him' Only the winner. \$\endgroup\$ Commented Nov 7 at 8:55

7 Answers 7

4
\$\begingroup\$

JavaScript (ES6), 41 bytes

Expects an array of 3 integers and returns an integer.

Mapping: 0, 1, 2, 3, 4 → Fire, Soil, Metal, Water, Wood.

a=>a[88614024>>a.sort().join``%37%14*2&3]

Try it online!

Method

Once the input array is sorted in ascending order, there are only 35 possible cases that can be identified with the concatenation of the values:

000, 001, ..., 333, 334, 344, 444

It turns out that it's quite efficient to use a modulo chain on this identifier, combined with a bitmask encoding the position of the answer in the sorted array.

It's worth noting that we may have several possibilities for the position (e.g. for 000 all positions are correct). This potentially allows us to optimize the collisions in the lookup data. But in practice, taking the first matching position is apparently just as good.


JavaScript (ES6), 39 bytes

We can exploit the loose I/O format to save a few bytes. But I don't really like doing so.

Mapping: 0, 2, 4, 6, 8 → Fire, Soil, Metal, Water, Wood.

a=>a[88614024>>a.sort().join``%74%28&3]

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 37 is unsurprising given that it's a factor of 111. \$\endgroup\$
    – Neil
    Commented Nov 7 at 13:30
  • \$\begingroup\$ @Neil Yes, what really matters is the difference between the values, so 112 is essentially the same case as 334 for instance. (And the only identifier for which it makes a difference to reduce modulo 37 rather than modulo 111 is 044.) \$\endgroup\$
    – Arnauld
    Commented Nov 7 at 14:08
3
\$\begingroup\$

05AB1E, 16 14 12 bytes

Σ-5%2LδQÆO}θ

Mapping: Wood=1; Water=2; Metal=3; Soil=4; Fire=5.

Try it online or verify all possible combinations.

Explanation:

Σ          # Sort the (implicit) input-triplet by:
 -         #  Subtract the current value from each value in the (implicit) input
  5%       #  Modulo-5 each (-4,-3,-2,-1,0,1,2,3,4 becomes 1,2,3,4,0,1,2,3,4)
    2L     #  Push pair [1,2]
      δ    #  Apply double-vectorized:
       Q   #   Equals check
           #  (==1 are +1 point cases; ==2 are -1 point cases)
        Æ  #  Reduce each inner-most pair of checks by subtracting
         O #  Sum this triplet together
}θ         # After the sort-by: pop and keep the last/maximum item
           # (which is output implicitly as result)
\$\endgroup\$
3
\$\begingroup\$

Uiua, 22 chars

=⟜/↥×⊃(+1-⊃(↻2|↻1))±/+

Pad Link

Represents the elements as follows:

Fire  ← 0_0_0_0_1
Soil  ← 0_0_0_1_0
Metal ← 0_0_1_0_0
Water ← 0_1_0_0_0
Wood  ← 1_0_0_0_0

Basic explanation:

total = sum(input)
mask = sign(s) # filter non-zero elements
scores = rotate(total, 1) - rotate(total, 2) + 1
masked_scores = scores * mask
max_value = max(masked_scores)
output = max_value == masked_scores
\$\endgroup\$
2
\$\begingroup\$

Perl 5 -MList::Util=sum -pF, 63 bytes

s;.;$a[5+sum map{($b=($_-$&)%5)==1||-($b==2)}@F]=$&;ge;$_=pop@a

Try it online!

Input as three digits with no spaces:

4   Fire
3   Soil
2   Metal
1   Water
0   Wood

Output uses the same encoding.

\$\endgroup\$
1
\$\begingroup\$

Retina 0.8.2, 100 bytes

O`.
$
¶$`
T`d`0670`.+$
.
$*_¶
O$^`(_+)(?=¶+\1(_)\b)?((?!(¶+_+)*¶+\1__\b))?
$#2$*1$#3$*
1G`^_{1,5}$
_

Try it online! Takes input as a string of digits 1-5 representing Wood, Water, Metal, Soil, Fire but link is to test suite that converts from and to words for convenience. Explanation:

O`.

Sort the digits.

$
¶$`

Make a copy of the digits.

T`d`0670`.+$

Change 1 and 2 to 6 and 7 and the other copies to 0.

.
$*_¶

Convert to unary.

O$^`(_+)(?=¶+\1(_)\b)?((?!(¶+_+)*¶+\1__\b))?
$#2$*1$#3$*

Sort descending by points plus one ($#3 is 1 if there is no negative action).

1G`^_{1,5}$

Take the first, but take care not to take 6 or 7 by mistake, as they are only used for scoring purposes.

_

Convert to decimal.

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 24 bytes

Fθ⊞υ↨E²№θI﹪⁺κ⊕ι⁵±¹§θ⌕υ⌊υ

Try it online! Link is to verbose version of code. Takes input as a string of digits 1-5 representing Wood, Water, Metal, Soil, Fire. Explanation:

Fθ

Loop over each digit.

⊞υ↨E²№θI﹪⁺κ⊕ι⁵±¹

Count the number of times the incremented and double incremented digit appears (modulo 5) in the input string, and take the difference (but the wrong way around, so that the points are negated).

§θ⌕υ⌊υ

Output the digit with the minimum negated points.

\$\endgroup\$
1
\$\begingroup\$

Python 2, 52 bytes

lambda i:i[88614024>>int(`sorted(i)`[2::5])%74%28&3]

Try it online!

This is Arnauld's answer which I translated into Python.

\$\endgroup\$

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.