PHP, 273 258 255 248 256 239 234 bytes
What a mess.
Submission:
function n($s,$b){$r=$q=$f="";for($g=-1;$g++<$b;){$i=0;foreach(explode(" ",$s)as$d){$i+=in_array($d,str_split($g));}if($i&&$b-$g){$q[]=$g;$f=$f?:$g;}elseif($f){$r.=($g-$f>9?$q[0]." --> ".($g-1):implode(" ",$q))." ";$q=$f="";}}echo$r;}
Exploded view:
function n($s, $b) {
$r = $q = $f = "";
for ($g = -1; $g++ < $b;) {
$i = 0;
foreach(explode(" ", $s) as $d) {
$i += in_array($d, str_split($g));
}
if ($i && $b-$g) {
$q[] = $g;
$f = $f ?: $g;
} elseif ($f){
$r .= ($g-$f > 9 ? $q[0] . " --> " . ($g-1)
: implode(" ", $q) ) . " ";
$q = $f = "";
}
}
echo $r;
}
Variable explanations:
$b
: string, upper bound (input)
$s
: string, digit string (input)
$d
: int, individual digit in $s
$g
: int, current guess
$i
: int, number of times a digit exists in the current guess
$f
: int, first correct guess in a chain
$q
: array, correct guess queue
$r
: string, final result (output)
This is super unoptimized, still tweaking it.
Edits:
-18: Combining variable inits, merging two $i
inits.
-7: Removing $a
declaration.
+8: Fixed two bugs: now allows 0
as output, now allows $b-1
and --> $b-1
as output.
-17: God, I love ternary operators.
-5: Let's treat $i
as an int
full-time, and simplify the resets.
->
or would a Unicode arrow like→
also be fine? \$\endgroup\$-->
. \$\endgroup\$