s/1.*/ax/
s/2.*/axbx/
s/3.*/axbxcx/
$!{
:
N
/1 0/{
:0
$!N
$!b0
c-1
}
s/\n1/\na/
s/\n2/\nb/
s/\n3/\nc/
s/\([a-c]\)x\(.*\)\n\1 \(.\)/\1\3\2/
s/\([a-c]\)\([0-9]\)\(.*\)\n\1 \2/\1\2\3/
/\n/b0
$!b
}
s/.\(.\)/\1/g
s/x/0/g
s/^0\(..*\)/1\1/