Retina 0.8.2, 5555 53 bytes
\d
$*____ab
_
bc
(.)\1
(b|cac)a
a$1
}`(cbcbca?cbcbc)b
b$1
a
Try it online!Try it online! Link includes test cases. Explanation: Another port of @AndersKaseorg's Perl answer. Edit: Saved 2 bytes using @alephalpha's observation.
\d
$*____ab
_
bc
First expand each digit into n+3
underscores followed by ab
, then replace each underscore with bc
.
(.)\1
(b|cac)a
a$1
(cbcbca?cbcbc)b
b$1
Perform the simplifications.
}`
Repeat until no more simplifications can be made.
a
Count the remaining number of a
s.