You get on stdin one integer per line. The first integer is the target. Find in the rest the number closest to the target and print it to stdout followed by a newline, nothing should go to stderr. No values will be equally close to the target. Each line matches /^[1-9]\d{0,3}\n\z/. Input will be at least two lines.
example input:
8 12 6 13 5here 8 is the target number, 6 is closest (distance two), so the output should be:
6
35 darek, ton-, tybalt89 37 mtve 38 dave, depesz, oozy 40 spiff 41 suo 44 banshee1, sorroww 46 lard 50 TopMach 53 andys
ton- 35 -p ($_)=sort{abs$a-$_<=>abs$b-$_}<> tybalt89 35 -p ($_)=sort{abs$a-$_<=>abs$b-$_}<> darek 35 -p ($_)=sort{abs$a-$_<=>abs$b-$_}<> mtve 37 -p $??$\x=abs$?-$\<abs$?-$_:$?||=$_}{ depesz 38 -p $_=(sort{(abs$a-$_)-abs$b-$_}<>)[0] dave 38 -p $_=(sort{(abs$a-$_)-abs$b-$_}<>)[0] oozy 38 -p $_=(sort{abs$a-$_<=>abs$b-$_}<>)[0] spiff 40 -p $n?$a[abs$n-$_]:$n=$_}{($_)=grep$_,@a suo 41 -p for$k(<>){$x[1E5-abs$k-$_]=$k}$_=pop@x banshee1 44 $h=<>;print+(sort{abs$a-$h<=>abs$b-$h}<>)[0] sorrow 44 $x=<>;print+(sort{abs$a-$x<=>abs$b-$x}<>)[0] lard 46 $h=<>;print@{[sort{abs$a-$h<=>abs$b-$h}<>]}[0] TopMach 50 $v=<>;$a[($v-$_)**2]=$_ for<>;print"@a"=~/([^ ]+)/ (might break the 2**32 rule) andys 53 -l $n=<>;print$n+(sort{abs$a<=>abs$b}map{$_-$n}<>)[0]After the golf this was found:
mtve 32 -p ($_)=sort{$a-$b^$a+$b-2*$_}<>the body is derived from:
($a-$_)**2-($b-$_)**2 $a**2-2*$a*$_-$b**2+2*$b*$_ $a**2-$b**2-2*$_*($b-$a) ($a-$b)*($a+$b)-2*$_*($b-$a) ($a-$b)*($a+$b-2*$_)And only the sign matters (and 0 does not happen), so replace * by ^. This gets you very big positive numbers instead of negative numbers, but perl's internal sort uses them as a signed value, so it maps to negative again.