# AnyDice: Count successes plus a limited number of easier successes

I've been searching around but haven't yet found how to get this average out of AnyDice.

Players roll a pool of d6s. Normally, a success is counted on a 4, 5, or 6. However, there's a condition a player may have where a certain number of 3s also count as successes.

How can I get this into AnyDice? For example, what is the expected number of successes if I roll 3d6, but I can count up to two 3s as successes?

In other words: Total successes = 4s + 5s + 6s + (3s, but a limit of two)

# This script works

function: successes in N:s up to M threes {  \ ':s' expects a sequence: the function is invoked for all possible sequences that can be made by rolling dice \
result: [count {6,5,4} in N]               \ count 6s, 5s, and 4s \
+ [lowest of M and [count 3 in N]]   \ plus count at most M 3s \
}

loop N over {1..5} {
output [successes in Nd6 up to 2 threes] named "Successes in [N]d6"
}


The key is to use the ':s' so that the function is called for each possible sequence that the dice can roll. From there it's easy to use 'count values in sequence' and 'lowest of number and number' to count successes with with an upper bound to 3s.

Here's the 'at least' results, for 1d6 through 5d6 (counting up to 2 3s):