4
\$\begingroup\$

Background

A Medusa have released a dangerous Hydra which is revived unless the exact number of heads it have is removed. The knights can remove a certain number of heads with each type of attack, and each attack causes a specific amount of heads to regrow. The knights have hired you to write a program or function that returns a truthy/falsey depending on whether the Hydra can be left with exactly zero heads after the next hit.

Note that this is fundamentally different from Become the Hydra Slayer. You are not supposed to figure out which attacks to use.

For example:

input: head = 2, 
       attacks = [1, 25, 62, 67], 
       growths = [15, 15, 34, 25], 
       hits = [5, 1, 0, 0]
output: TRUE

Explanation: The Hydra has 10 heads to start with, we have 4 different attacks and for each attack, growth gives us the number of heads that grows back. hits gives us the number of times each attack is applied. So the number of heads the Hydra has after each attack is

2 -> 16 -> 30 -> 44 -> 58 -> 72 -> 62

Since 62 is a valid attack value (It lies in the attack list), we return True since the Hydra will die on the next attack (be left with 0 heads). Note that the order for when the attacks are done is irrelevant.

2 -> 16 -> 6 -> 20 -> 34 -> 48 -> 62

Input

Any sort of logical way of feeding your program the attack, regrowth and hit values are acceptable. This includes, but is not limited to

  • A list of tuples (1, 15, 5), (25, 15, 1) (62, 34, 0) (67, 25, 0)
  • Lists 2, [1, 25, 62, 67], [15, 15, 34, 25], [5, 1, 0, 0]
  • Reading values from STDIN 1 15 1 15 1 15 1 15 1 15 25 15
  • A file of values

Output

  • Some form of truthy/falsey value in your language: 0/1, true/false, etc.

Assumption

  • You may assume that any input is valid. E.g every input will not overkill the Hydra and either result in a number of heads that is an attack value, or not.

  • Every list (if used) is to be assumed to be of equal lengths

  • Every attack value will always correspond to one regrowth value which never changes. these are not required to be unique

  • Every input will be a positive integer


Test Cases

The following 10 examples are all True, and uses attacks=[1, 25, 62, 67], growths=[15, 15, 34, 25] these are simply left out to be brief

1, [0, 0, 0, 0], -> True
2, [5, 1, 0, 0], -> True
3, [2, 3, 0, 0], -> True
4, [7, 4, 0, 0], -> True
5, [4, 6, 0, 0], -> True
6, [4, 0, 0, 0], -> True
7, [1, 2, 0, 0], -> True
8, [6, 3, 0, 0], -> True
9, [3, 5, 0, 0], -> True
10, [8, 6, 0, 0] -> True
25, [0, 0, 0, 0], -> True
67, [0, 0, 0, 0], -> True
62, [0, 0, 0, 0], -> True
98767893, [0, 1, 1, 2351614] -> True

The following examples are all False, and uses attack=[1, 25, 62, 67], growth=[15, 15, 34, 25] these are simply left out to be brief

65, [0, 3, 0, 0], -> False
66, [4, 5, 0, 0], -> False
68, [0, 2, 0, 0], -> False
69, [0, 1, 1, 0], -> False
70, [2, 5, 0, 0], -> False
71, [0, 0, 0, 1], -> False
72, [0, 0, 0, 0], -> False
73, [1, 2, 1, 0], -> False
74, [2, 3, 0, 0], -> False
75, [1, 5, 0, 0], -> False
98767893, [1, 1, 1, 2351614] -> False

This is a question so answers will be scored in bytes, with fewer bytes being better.

\$\endgroup\$
3
  • \$\begingroup\$ Just remove the heart. \$\endgroup\$
    – Joshua
    Jul 1 at 14:18
  • \$\begingroup\$ Are only regrowth values not guaranteed to be unique, or are attack values not necessarily unique as well? \$\endgroup\$
    – att
    Jul 4 at 18:38
  • \$\begingroup\$ @att Neither Attack nor regrowth values are necessarily unique \$\endgroup\$ Jul 4 at 18:47
6
\$\begingroup\$

Haskell, 44 bytes

w=zipWith
(u#z)y=elem=<<sum.(u:).w(*)z.w(-)y

Try it online!

Pretty simple. We get the differences of the attacks and the regrows with zipWith(-), then we multiply these by the attacks taken zipWith(*)z. Then we sum that with the starting head count, and check if that is in the attack list.

\$\endgroup\$
3
\$\begingroup\$

Vyxal, 5 bytes

-*∑+c

Try it Online!

Inputs: attacks, growths, hits, heads

Explanation

- subtract: growths - attacks
* multiply: that * hits
sum
+ add: that + heads
c contains: is it in attacks?

\$\endgroup\$
3
\$\begingroup\$

R, 36 bytes

function(H,a,g,h)sum(H,g*h-a*h)%in%a

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Jelly, 7 6 bytes

_ḋ⁵⁶+e

Try it online!

Takes four arguments: growths, attacks, hits, heads.

Explanation

_ḋ⁵⁶+e Main dyadic link
_      Subtract: growths - attacks
 ḋ     Dot product with
  ⁵      third argument: hits
    +  Add
   ⁶     fourth argument: heads
     e Is it an element of the second argument: attacks?
\$\endgroup\$
3
\$\begingroup\$

Python 3, 56 bytes

Try it online!

lambda h,a,g,t:h+sum(z*(x-y)for x,y,z in zip(g,a,t))in a
\$\endgroup\$
1
  • 1
    \$\begingroup\$ In your TIO link you should put the tests in the footer and the naming of your function in the header like so. Good answer though :) \$\endgroup\$
    – Jakque
    Jul 4 at 10:50
2
\$\begingroup\$

JavaScript (ES6), 54 bytes

Returns a Boolean value.

(a,g,n,h)=>a.includes(h.map((v,i)=>n+=v*=g[i]-a[i])|n)

Try it online!

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

Wolfram Language (Mathematica), 22 bytes

FreeQ[#,#3-#4.(#-#2)]&

Try it online!

Input [attacks, growths, hits, head]. Returns True if the hydra cannot be slain on the next hit.

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

Charcoal, 16 bytes

№η⁺θΣEε×ι⁻§ζκ§ηκ

Try it online! Link is to verbose version of code. Outputs one - for each matching attack, so if there are two attacks that deal the same amount of damage then -- will be output. Explanation:

      ε             Array of hits
     E              Map over elements
          §ζκ       Relevant growth
         ⁻          Subtract
             §ηκ    Relevant attack
       ×            Multiplied by
        ι           Current element
    Σ               Take the sum
  ⁺θ                Add the starting number of heads
№η                  Output the number of matching attacks
\$\endgroup\$
1
\$\begingroup\$

J, 15 bytes

Just to remind myself that / supports cycling gerunds. Takes health on the left, (hits, regrow, attack) on the right.

(+1#.*`-/)e.2{]

Try it online!

  • *`-/ Does hits * regrow - attack
  • 1#. summed
  • + plus health
  • e.2{] exists in the attacks?
\$\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.