DEX Save

Purple d20, public domain
Oh no! After months of playing a campaign in the table-top role-playing game Bungalows & Banshees, your character accidentally triggered a trap and was squished under a gigantic rolling boulder. If you had only succeeded in that dexterity saving throw to dodge out of the way of the incoming boulder of doom...

Always one to dwell on the past, you begin to wonder what your chances actually were of succeeding with the saving throw, and discover that it is not immediately obvious – you had that bardic inspiration giving you a d6 bonus, but then you had disadvantage on the roll due to being intoxicated, but on the other hand your dexterity save modifier was pretty high...

A basic dexterity saving throw with difficulty $d$ is performed in the following way: first, a d$20$ (i.e., a $20$-sided die) is rolled. If the result of the roll is $1$ the result is immediate failure, and if the result is $20$ the result is immediate success. Otherwise, the character’s DEX save modifier $m$ is added to the die result. If the sum is at least the difficulty $d$, the result is a success, otherwise it is a failure.

There are two extensions to this. First, a roll may be made with either advantage or disadvantage. In these cases, two d$20$s are rolled instead of one, but then only the highest (for advantage) or lowest (for disadvantage) is kept and the result is then computed as in the basic case. Second, the roll may have additional bonuses or penalty in the form of additional dice that are rolled and then added to or subtracted from the d20 result before comparing it to $d$ (but these bonuses/penalties are only rolled once regardless of advantage or disadvantage).

Write a program which, given the data of a saving throw (its difficulty, the DEX save modifier, advantage/disadvantage status, and additional bonus dice) computes the probability that the saving throw will succeed.


The input consists of three lines. The first line contains two integers $d$ and $m$ ($0 \le d \le 30$, $-10 \le m \le 10$), the difficulty of the roll and the DEX save modifier of the character. The second line contains one word indicating if the roll has advantage or disadvantage. This word is either “straight” (for a straight roll with neither advantage or disadvantage), “advantage”, or “disadvantage”. Finally, the third line starts with an integer $k$ ($0 \le k \le 5$) indicating the number of additional bonus/penalty dice. This is followed by $k$ dice descriptions, each of the form “[+-]d$x$” ($3 \le x \le 10$ is an integer) indicating that we add (if ‘+’) or subtract (if ‘-’) the outcome of an $x$-sided die to the result.


Output a single number, the probability that a dexterity saving throw with the given parameters will succeed. This number should be given with an absolute error of at most $10^{-6}$.

Sample Input 1 Sample Output 1
12 4
Sample Input 2 Sample Output 2
10 3
Sample Input 3 Sample Output 3
20 7
2 +d6 -d4
CPU Time limit 2 seconds
Memory limit 1024 MB
Difficulty 5.3medium
Statistics Show
License Creative Commons License (cc by-sa)

Please log in to submit a solution to this problem

Log in