Dice formula evaluation
A game server replays a match from a recorded stream of integers so that every replay lands identically. Each entry of formulas is terms joined by "+" or "-" with no spaces, and the first term carries no sign. A term is either a plain integer or a dice group "NdM", where M is the face count and N is how many dice, defaulting to 1 when it is left off. Every die consumes the next value v in stream, in the order the dice appear, and shows (v mod M) + 1. A group of zero dice consumes nothing and adds nothing. The stream position carries on from one formula to the next, and stream always holds enough values. Return the total of each formula, in the order the formulas are given.
roll_formula_totals(formulas: list[str], stream: list[int]) → list[int][["2d6+3","1d20","4"],[11,5,0,19,7]]out[15,1,4][["d8+d8","10-1d4"],[7,0,6]]out[9,7][["3d6","0d6+5","d10-2"],[0,1,2,41]]out[6,5,0]State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
[["2d6+3","1d20","4"],[11,5,0,19,7]][15,1,4]not run yetsample[["d8+d8","10-1d4"],[7,0,6]][9,7]not run yetsample[["3d6","0d6+5","d10-2"],[0,1,2,41]][6,5,0]not run yetsample