# RogerBW's Blog

Perl Weekly Challenge 43: rings and self-descriptive numbers 09 February 2020

I’ve been doing the Perl Weekly Challenges. The latest was about allocating numbers to rings and generating self-descriptive numbers.

The difficulty with the first problem was in working out what was meant to be going on. After that it was just a matter of allocating each number to each slot and checking the result. No clever coding here, and since giving code would spoil the problem, I won't. I found three solutions that satisfy the constraint.

For the second, I used an iterative approach for bases 4 and 5 (start with "1" followed by a relevant number of "0", count digits and construct a new number, repeat until stable). For bases 7+, the initial seed is changed to a known good result, because starting with 1000… doesn't necessarily converge on a valid answer.

``````my @n=(0) x \$base;

\$n=1;

if (\$base>6) {
\$n=\$base-4;
\$n=2;
\$n=1;
\$n[\$base-4]=1
}
while (1) {
my @o=@n;
my %o;
map {\$o{\$_}++} @o;
foreach my \$i (0..\$#o) {
\$n[\$i]=\$o{\$i} || 0;
}
if (join('',@o) eq join('',@n)) {
last;
}
}
``````