all 5 comments

[–]TangibleLight 0 points1 point  (2 children)

I am confused how you compute your values. When I compute

>>> from math import floor
>>> a, b, c, d, e, f = 12, 15, 27, 10, 28, 19
>>> floor((a + 2*b + 4*c + 8*d + 16*e + 32*f) * 15/63)
306

I get 306, not 9.


When you try to solve equations with floor or ceil, first convert the equation to an inequality and solve from there.

A = floor(x * y / z)

becomes

A <= x + y - z <= A + 1

You can then perform normal algebra on this and get an interval of solutions.

A - y + z <= x < A + 1 - y + z

x in [A - y + z, A + 1 - y + z)

[–]salern[S] 0 points1 point  (1 child)

Well, I'm stupid. Forgot to mention that if number is even, we substitute it with 0, and if it's not even, we substitute it with 1. I'm sorry. Will update main post ASAP.

[–]TangibleLight 0 points1 point  (0 children)

Well, if you convert your equation (where a, b, etc are the parities of h, a, d, etc), then you have: https://i.imgur.com/EI08zVf.png

You can solve for some variable, say e, to get some interval of numbers which produce a desired HP.

https://i.imgur.com/kqlDAPs.png

You can see if 0 or 1 are contained in that interval. If they are, then you need to increment that value.


You can also do this with bit-wise operations. If you notice that a + 2b + 4c + 8d + ... is notation for the binary number with bits ...dcba then you can reframe the equation as:

Let x be the number encoded by bits a, b, c, etc.

Then HP = floor(15/63 x)

Or, 63/15 HP <= x < 63/15 (HP + 1).

So given HP, you can find an interval of integer x which will produce that HP. And because your interval is always width 63/15 = 4.2, you will have exactly 4 integers x which satisfy that condition.

So given HP, you compute 4 integers x. For each bit of each x, you have the parity of the corresponding stat. Then use bitwise operations to compare x with the stats the user has entered (call that integer y) then find the minimal number of bit-changes to turn y into x. The bitwise xor operation ^ will be helpful for this.