sedの数値演算はいつもanagolの過去問からコピペしてる。

implementation

#!/bin/sed -f
:
s/[1-9]/&-/g
y/123456789/012345678/
s/-0/0----------/
t
s/00*/0/g
:1
/0 0-.*0 0-/s/^/+/
s/0-/0/g
t1
s/0.*//
s/^/0/
:2
s/+/<<123456789+01>/
s/\s*\(.\)<.*\1\(+*.\).*>/\2/
t2