The system uses a dice pool of d4s of up to 3 dice (3d4 is the maximum number of dice you can roll). If at least a 4 comes up in the pool, that's a success. If the highest number is a 3, it's a partial success. If the highest is lower than 3 it's a failure.

```
Output [count {3..4} in 3d4]
```

I was able to calculate that with ease, but here's the part I am stuck with: If you get any number of 1s in your pool, the highest die is rerolled if it's a 3 or a 4. The second result is kept. Examples:

- You roll 2d4 and get 4, 1. Since you got a 1, you reroll the 4 and keep the second result. We get a 3, which means the result is a partial success instead of a success. 

- You roll 3d4 and get 3, 1, 1. Since there's a 1, you reroll the 3 and get 4. Lucky you, you got a success instead of a partial.

How can I code this in AnyDice to accurately calculate a player's chance of success, partial success, or failure?