Compute my Sacred Geometry

8

1

In the tabletop RPG named Pathfinder, there is a feat that characters can take called Sacred Geometry, which allows a character who has it to buff their spells in exchange for doing some math: to use it, the character rolls a number of six-sided dice equal to their ranks in a particular skill, consults a table based on their spell level to determine which three prime numbers are the "Prime Constants" for that spell level, then calculates whether or not it's possible to produce one of the Prime Constants by performing some combination of addition, subtraction, multiplication, and division and parenthetical grouping on all the numbers rolled.

The table of Prime Constants by Spell Level are as follows:

+-------------+-----------------+
| Spell Level | Prime Constants |
+-------------+-----------------+
| 1st         | 3, 5, 7         |
| 2nd         | 11, 13, 17      |
| 3rd         | 19, 23, 29      |
| 4th         | 31, 37, 41      |
| 5th         | 43, 47, 53      |
| 6th         | 59, 61, 67      |
| 7th         | 71, 73, 79      |
| 8th         | 83, 89, 97      |
| 9th         | 101, 103, 107   |
+-------------+-----------------+

So, for instance, if a character has 5 skill ranks and they're casting a 4th level spell, they would roll five six-sided dice and would need to be able to calculate a value of 31, 37, or 41. If they rolled a 6, 6, 4, 3 and 1, then they could produce a value of 37 by performing the following computation: (6 × 6) + (4 – 3) × 1 = 37, or they could produce a value of 41 by doing ([6 + 6] × 3) + 4 + 1 = 41. As a result, their casting of the spell would succeed.

For this programming puzzle, your job is to write a function with two input parameters, Spell Level and Skill Ranks, roll a number of six-sided dice equal to the Skill Ranks parameter, then compute if you can produce (at least) one of the Prime Constants associated with the Spell Level parameter, then output a Boolean value.

Answers would be ranked primarily by the efficiency of their algorithm (I'm pretty sure that a brute force algorithm would scale very quickly as the Skill Ranks parameter increases), and then by the size of the submitted source code in bytes.

nick012000

Posted 2019-07-10T18:28:48.800

Reputation: 181

Question was closed 2019-07-11T04:05:12.547

I don't think Pathfinder requires the character to roll dice, but rather the player, right? – mbomb007 – 2019-07-10T19:01:09.617

3Also, how do you intend to rank the efficiency of the algorithm someone uses? – mbomb007 – 2019-07-10T19:04:18.697

Just out of curiosity, why is the program rolling for you? Wouldn't it be more useful if you provided the roll results rather than the skill rank (not to mention easier to test)...? – Triggernometry – 2019-07-10T19:44:45.610

1

@mbomb007 There's always a relevant xkcd.

– Khuldraeseth na'Barya – 2019-07-10T21:12:29.403

Triggernometry is right. Who's to say my program's dice aren't fair? Meta discussion and another relevant xkcd.

– Khuldraeseth na'Barya – 2019-07-10T21:15:31.350

@Khuldraesethna'Barya https://codegolf.meta.stackexchange.com/a/1739/34718

– mbomb007 – 2019-07-10T21:36:15.437

2By default randomness doesn't have to be fair, but it does have to encompass all possibilities. However, in this case, the extra steps inbetween are unobservable, so basically a submission would only have to randomly output true or false. It would be better if answers took the dice values as input instead – Jo King – 2019-07-10T22:02:52.073

A lookup table would be pretty fast. If you sort the dice there are only 252 possible rolls. – recursive – 2019-07-11T03:06:50.673

So, would I be correct in guessing that this question was closed since the random numbers are generated inside the program, and you can't see what they are to verify them? I could edit the question to have it output the random numbers as well as the boolean value, but not all languages allow multiple return values. – nick012000 – 2019-07-11T08:09:12.750

@nick012000: I'm not sure why it was closed. I didn't vote on it. But I do think it would be a better challenge if it the program was required to take the dice rolls and spell levels as input. – recursive – 2019-07-11T17:55:05.940

Answers

1

Python 2.7, 285 bytes

from itertools import*
from random import*
def f(s,r):
 p=[];n=1;k=1;P=1;d=choices('123456',k=r);D=permutations(d,r);O=product('+-*/',repeat=r-1)
 while-~s*3>n:p+=P%k*[k];n,k,P=n+P%k,k+1,P*k*k
 return any(n in{eval('int(%s)'%'%s'.join(i)%o)for i in D for o in O}for n in p[-4:-1])

Technically conforms to the specifications of the challenge. Only returns True/False, does not expose the dice rolls used or the expression used to caller.

Also, this algorithm ignores grouping, since we can perform the same operations without grouping by using ordering instead. (This would not be true if exponentiation was one of the possible operations, for example.)

Exponential runtime, since it generates all possibilities and then checks if at least one solution exists.

Triggernometry

Posted 2019-07-10T18:28:48.800

Reputation: 765