add_four_digit = ("0":"4") | ("1":"5") | ("2":"6") | ("3":"7") | ("4":"8") | ("5":"9") | ("6":"01") | ("7":"11") | ("8":"21") | ("9":"31"); add_one_digit = ("C0":"1") | ("C1":"2") | ("C2":"3") | ("C3":"4") | ("C4":"5") | ("C5":"6") | ("C6":"7") | ("C7":"8") | ("C8":"9") | ("C9":"0C"); digit = "0" | "1" | "2" | "4" | "5" | "6" | "7" | "8" | "9"; number = digit+; add_four = add_four_digit number?; handle_carry = (number? (("S0":"0") | ("S1":"1") | ("S2":"2") | ("S3":"3")) number?) | number; #handle_carry = (number? (("S0":"0C") | ("S1":"1C") | ("S2":"2C") | ("S3":"3C")) number?) | (number? (("C0":"1") | ("C1":"2") | ("C2":"3") | ("C3":"4") | ("C4":"5") | ("C5":"6") | ("C6":"7") | ("C7":"8") | ("C8":"9") | ("C9":"0C")) number?) | (number? ("C":"1")) | number; export PROCESS = Optimize[Reverse[add_four @ handle_carry]];