for n in {1..20}; do printf "%d %d\n" $n $(perl -pe'$a.=/(.*)(.)\1$/^$2for($a)x$_;$_=$a%5+1' <<< $n)done
Standard input is empty
1 1 2 2 3 1 4 1 5 2 6 2 7 1 8 2 9 1 10 1 11 2 12 1 13 2 14 2 15 1 16 1 17 2 18 1 19 1 20 1
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!