I did a bit better in 10 minutes:
I used the Hospel-method of avoiding long commands, and tried to get rid of
the print. How do we do that? with -p. But we can't use -p, because it'll
print $_ for each line!
Aha! If we call <> within the -p script, that's like calling <> within a
"while(<>){}" loop, which is, as the MLM emails say, 100% LEGAL. Better, if
we call <> in array context within the -p, then it will put the first thing
into $_, and read all the rest into the array. Then all we need to do is set
$_ to be the new value, and it will be printed at the end! So:
-p $_=(sort{abs$_-$a<=>abs$_-$b}<>)[0] (38)
Actually, my solution tried to be smart by using minus inside the sort
instead of <=>. However, that requires you to use parentheses to get the
precedence right, so it takes two more characters.
In other news, I've just learned that sort called in scalar context returns
undef, so we can't just hope it'll return the last or first value or
something.
After a bit of tinkering with equations and stuff, I realized that
abs($a-$m) is sqrt(($a-$m)**2) = sqrt($a**2-2$m$a+$m**2). So instead of
comparing abs', we can compare the squared things. (If the sqrt is bigger,
the square will be bigger.)
$a**2-2$m$a+$m**2 <=> $b**2-2$m$b+$m**2
is long, but we can get rid of $m**2
$a**2-2$m$a <=> $b**2-2$m$b
A bit of algebra and, using the minus trick instead of <=>, you get
-p $_=(sort{($a-$b)*($a+$b-2*$_)}<>)[0] (39)
This was shorter than my try, but I guess isn't shorter than the 38 above.o
Finally, I used the knowledge from the last TPR to try the relationship
between ^ and subtraction. I got:
-p $_=(sort{1x$a^1x$_-1x$b^1x$_}<>)[0]
38, but it only works with non-negative integers.
That's enough work in a non-TPR week for me.
-Amir