The first part of your problem: Values in A2:A12 enter in B2 and fill down:

Formula: copy to clipboard
=IF(A2<>A1,MAX($B$1:B1)+1,B1)


A
B
2
A
1
3
A
1
4
B
2
5
B
2
6
B
2
7
C
3
8
C
3
9
D
4
10
D
4
11
D
4
12
D
4