Buggy python submission (verdict: Time Limit Exceeded):
s = list(input())
c = 0
state = 0
res = 0
stack = 0
loop = 0
while c < len(s):
    """
    loop = loop + 1
    if loop > 100:
        break
    print("c" + str(c))
    print("cur: " + "".join(s))
    print("state" + str(state))
    """
    if state == 0:
        if s[c] == "A":
            state = 1
            stack = 0
    elif state == 1:
        if s[c] == "B":
            state = 2
        else:
            if s[c] == "A":
                state = 1
                stack += 1
                #print("stack up:" + str(stack))
            else:
                state = 0
                stack = 0
    elif state == 2:
        if s[c] == "C":
            #print("hit")
            #print(" stack:" + str(stack))
            res += 1 + stack
            s[c-2-stack], s[c-1-stack], s[c-stack] = "B", "C", "A"
            for i in range(stack):
                s[c-i] = "A"
            c = c - 3 - stack
            state = 0
            stack = 0
            if s[c] == "A":
                state = 1
                stack = 0
        else:
            state = 0
            stack = 0
            if s[c] == "A":
                state = 1
                stack = 0
    c = c + 1
print(res)

Fixed submission:
import re
s = input()
s = s.replace("BC", "X")
m = re.search(r"[AX]+", s)
count = 0
res = 0
for m in re.finditer(r"([AX]+)", s):
    t = str(m.groups()[0])
    t = ''.join(list(reversed(t)))
    count = 0
    for i in range(len(t)):
        if t[i] == "A":
            res += (i - count)
            count += 1
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
s = list(input())
c = 0
state = 0
res = 0
while c < len(s):
    #print("c" + str(c))
    #print("cur: " + "".join(s))
    #print("state" + str(state))
    if state == 0:
        if s[c] == "A":
            state = 1
    elif state == 1:
        if s[c] == "B":
            state = 2
        else:
            state = 0
            if s[c] == "A":
                state = 1
    elif state == 2:
        if s[c] == "C":
            #print("hit")
            res += 1
            s[c-2], s[c-1], s[c] = "B", "C", "A"
            c = c - 3
            state = 0
        else:
            state = 0
        if s[c] == "A":
            state = 1
    c = c + 1
print(res)

Fixed submission:
import re
s = input()
s = s.replace("BC", "X")
m = re.search(r"[AX]+", s)
count = 0
res = 0
for m in re.finditer(r"([AX]+)", s):
    t = str(m.groups()[0])
    t = ''.join(list(reversed(t)))
    count = 0
    for i in range(len(t)):
        if t[i] == "A":
            res += (i - count)
            count += 1
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
s = list(input())
c = 0
state = 0
res = 0
while c < len(s):
    #print("c" + str(c))
    #print("cur: " + "".join(s))
    #print("state" + str(state))
    if state == 0:
        if s[c] == "A":
            state = 1
    elif state == 1:
        if s[c] == "B":
            state = 2
        else:
            state = 0
            if s[c] == "A":
                state = 1
    elif state == 2:
        if s[c] == "C":
            #print("hit")
            res += 1
            s[c-2], s[c-1], s[c] = "B", "C", "A"
            c = c - 3
            state = 0
        else:
            state = 0
        if s[c] == "A":
            state = 1
    c = c + 1
print(res)

Fixed submission:
import re
s = input()
s = s.replace("BC", "X")
m = re.search(r"[AX]+", s)
count = 0
res = 0
for m in re.finditer(r"([AX]+)", s):
    t = str(m.groups()[0])
    t = ''.join(list(reversed(t)))
    count = 0
    for i in range(len(t)):
        if t[i] == "A":
            res += (i - count)
            count += 1
print(res)