| Bytes | Lang | Time | Link |
|---|---|---|---|
| 075 | Janet | 250318T133609Z | xigoi |
| 049 | Julia 1.0 | 230308T174949Z | MarcMush |
| 138 | Fortran GFortran | 230306T230832Z | roblogic |
| 009 | Vyxal ḋ | 230306T194028Z | lesobrod |
| 147 | C gcc | 201122T032746Z | ErikF |
| 079 | Python 3 | 201127T204652Z | garrison |
| 067 | Factor | 201125T080343Z | Bubbler |
| 082 | Python 3 | 201123T101925Z | Jitse |
| 077 | Perl 5 MListUtil=min | 201122T023728Z | Xcali |
| 022 | Charcoal | 201121T232436Z | Neil |
| 074 | Haskell | 201121T190956Z | AZTECCO |
| 014 | Brachylog | 201121T200634Z | Unrelate |
| 029 | J | 201121T194539Z | xash |
| 093 | Python 3 | 201121T030506Z | Noodle9 |
| 065 | Ruby 2.7 | 201121T061511Z | vrintle |
| 010 | Husk | 201121T155030Z | Dominic |
| 072 | Scala 3 | 201121T151455Z | user |
| 086 | JavaScript ES6 | 201121T091821Z | Arnauld |
| 014 | Japt g | 201121T125800Z | Shaggy |
| 034 | R | 201121T121339Z | Dominic |
| 017 | Octave/MATLAB with Statistics package/toolbox | 201121T115144Z | Luis Men |
| 008 | 05AB1E | 201121T083930Z | ovs |
| 033 | Wolfram Language Mathematica | 201121T044156Z | att |
| 009 | Jelly | 201121T030733Z | fireflam |
Janet, 89 75 bytes
|(min;(seq[a :in $ b :in $ :unless(= a b)](reduce2 math/hypot(map - a b))))
The points must be taken as arrays (not tuples) so that they are compared by reference.
Julia 1.0, 53 52 49 bytes
a>b...=min(>(b...),([a].>b)...)
a>b=hypot(a-b...)
This will work for bigger dimensions as well
Fortran (GFortran), 206... 138 bytes
function d(A,n);real A(3,n);t=huge(i)
do 5 k=1,n-1;do 5 j=2,n;s=0;do i=1,3;s=s+(A(i,k)-A(i,j))**2;enddo
5 if(s>0.and.t>s)t=s;d=sqrt(t);end
Try it online!
144
148
182
206
*line 1: sets up array, reads in data
*line 2: iterates over rows j,k, and sums the difference-squares
*line 3: compares current Euclidean squared distance s with tracking number t, taking the lower number, and ignores a distance of 0 (dupe points)
Saved 24 bytes by removing the allocate stuff and giving the array preset dimensions. A possible improvement for my other Fortran answers!
Saved another 34 bytes by using a subroutine and moving I/O to the main program. Replaced repeated enddos with a line number. Removed a placeholder variable q. Some inspiration taken from this fortran golf. I went looking for a useful intrinsic like NORM2 or HYPOT but couldn't find one for this problem.
Saved 4 bytes by replacing the subroutine with a function
Vyxal ḋ, 9 bytes
∪2ḋƛ÷∆d;g
∪ # Delete duplicates
2ḋ # All subsets of length 2
ƛ÷∆d; # Euclidean distance of every subset
g # Minimum
C (gcc), 155 147 bytes
Thanks to ceilingcat for -8 and an interesting (ab)use of hypot!
Takes a counted array of coordinates.
#define g(x)hypot(c[x+j]-c[x+i],
i,j;float f(s,c,d,l)float*c,d,l;{for(l=-1,s*=3,i=0;i<s;i+=3)for(j=i;(j+=3)<s;l=l<0|d<l?d:l)d=g(2)g(1)g()0)));s=l;}
Python 3, 79 bytes
I'm new here and not completely sure this follows the rules, LMK if this isn't valid!
lambda p:min(sum((a-b)**2for a,b in zip(x,y))**.5for x in p for y in p if x!=y)
Factor, 67 bytes
[ dup [ v- norm ] cartesian-map [ head ] map-index concat infimum ]
Evaluates self-cartesian-product by vector distance, takes the lower triangular matrix (without diagonals), and evaluates the minimum of all values.
[ ! anonymous lambda
dup [ v- norm ] cartesian-map ! self-cartesian-product by vector distance
[ head ] map-index ! for each array at index i, take first i elems
concat infimum ! minimum of all values
]
Factor, 73 bytes
USE: math.combinatorics
[ 2 [ first2 v- norm ] map-combinations infimum ]
Factor has a built-in for generating combinations, but it is way too long (USE: math.combinatorics map-combinations is already 40 bytes).
Python 3, 82 bytes
f=lambda a,*b:[*b]and[min([sum((x-y)**2for x,y in zip(a,q))**.5for q in b]+f(*b))]
Python 3 with SciPy, 58 bytes
lambda*a:min(pdist(a))
from scipy.spatial.distance import*
Perl 5 -MList::Util=min,sum, 81 77 bytes
@KjetilS shaved 4 bytes.
sub f{min map{@b=@$_;map{sqrt sum map($_-$b[$j++%3])**2,@$_}@_[++$i..$#_]}@_}
Charcoal, 22 bytes
I₂⌊ΦEθ⌊E…θκΣXEλ⁻ν§ιξ²κ
Try it online! Link is to verbose version of code. Takes n-dimensional vectors. Explanation:
θ Input list
E Map over vectors
θ Input list
… Truncate to length
κ Outer index
E Map over remaining vectors
λ Inner vector
E Map over coordinates
§ιξ Coordinate of outer vector
ν Current coordinate
⁻ Difference
X ² Squared
Σ Summed
⌊ Take the minimum square sum
Φ κ Filter out minimum of empty list
⌊ Take the minimum
₂ Take the square root
I Cast to string
Implicitly print
Haskell, 75 74 bytes
g[]=[]
g(x:t)=[(sum$zipWith((**2).)(map(-)x)z)**0.5|z<-t]++g t
f=minimum.g
g[]=[] - edge case for combinations
g(x:t)=[ ... |z<-t]++g t
- combinations
(sum$zipWith(\a b->(a-b)**2)x z)**0.5
- compute hypo..
f=minimum.g - return minimum value found
- Saved 1 thanks to @Unrelated String insane idea
(sum$zipWith(\a b->(a-b)**2)x z)**0.5becomes(sum$zipWith((**2).)(map(-)x)z)**0.5e.g. we first mapxto obtain a list of partially applied subtractions , then we zipWith(**2).(read "compose with square") which firstly finishes the subtraction and then computes the square. If I understood correctly O.o
Brachylog, 14 bytes
{⊇Ċz-ᵐ^₂ᵐ+√}ᶠ⌋
Boring and straightforward.
⌋ The output is the minimum of
{ }ᶠ every possible
√ square root of
+ a sum of
^₂ᵐ the squares of
-ᵐ the differences between
z the coordinates for each axis
⊇ for a sublist of the input
Ċ containing two elements.
J, 29 bytes
[:<./@,+/&.:*:@:-"1/~+_*[:=#\
[:<./@,+/&.:*:@:-"1/~+_*[:=#\
#\ 1…N
[:= identity matrix NxN
_* times infinity
+ plus
"1/~ the table with the coordinate triples:
@:- a - b and
&.:*: under square
+/ summed
&.:*: reverse square
[:<./@, flatten the table and get the min entry
Python 3, 106 95 93 bytes
Saved 11 bytes thanks to fireflame241!!!
Saved 2 bytes thanks to Jonathan Allan!!!
lambda l:min(sum((a-b)**2for a,b in zip(l[p],v))**.5for q,v in enumerate(l)for p in range(q))
Inputs a list of points as tuples and returns the Euclidean distance between the two closest points.
Works with points of any dimension so long as they are consistent within the list.
Ruby 2.7, 79 65 bytes
Saved a whooping 14 bytes, thanks to Sisyphus!
->s{s.combination(2).map{_1.zip(_2).sum{|a,b|(a-b)**2}**0.5}.min}
- Expects an array of points!
- TIO uses an older version of Ruby, so
|p,q|p,qis replaced by_1,_2to save three bytes (as suggested by Dingus).
Husk, 10 bytes
▼ṁẊȯ√ṁ□z-Ṗ
▼ # minimum of
ṁ # sums of applying function to
Ṗ # all subsets of input
# (this may include subsets of >2 points,
# but that's ok...)
Ẋȯ√ṁ□z- # the function:
Ẋȯ # apply to all adjacent pairs
# (...that's why it was ok if there were >2 points
# in any sublist)
√ # square root of
ṁ□ # sum of squres of
z- # element-wise differences
Scala 3, 72 bytes
s=>math.sqrt((for> <-s;| <-s- >yield(>zip|map(_-_)map(x=>x*x)).sum).min)
Accepts a Set[List[Int]] so that it can use - to ensure a point is not compared to itself.
This is my first time using Scala 3's new control syntax to save 2 bytes.
JavaScript (ES6), 86 bytes
a=>a.map(m=([x,y,z],i)=>a.map(([X,Y,Z])=>m=!i--|(d=Math.hypot(x-X,y-Y,z-Z))>m?m:d))&&m
Commented
a => // a[] = list of triplets
a.map(m = ([x, y, z], i) => // for each triplet (x,y,z) at position i in a[]:
a.map(([X, Y, Z]) => // for each triplet (X,Y,Z) in a[]:
m = // update the minimum distance m:
!i-- | ( // decrement i; if it was equal to 0
d = Math.hypot( // or the Euclidean distance d:
x - X, // between (x,y,z)
y - Y, // and (X,Y,Z)
z - Z //
)) > m ? // is greater than m:
m // leave m unchanged
: // else:
d // update m to d
) // end of inner map()
) && m // end of outer map(); return m
Japt -g, 15 14 bytes
The spec asks us to get the minimum but the lone test case gets the maximum so I don't know which to output. I've gone with the former but if that's not right then use the -h flag instead.
à2 ËÕËrnÃx²¬ÃÍ
R, 34 bytes
function(...)min(dist(rbind(...)))
This is a nice opportunity to use R's ... syntax to define a function that can accept a variable number of arguments; in this case, the x,y,z coordinates of each point.
The dist function calculates the pairwise distance between all rows of a matrix, using a chosen method - luckily, the default is 'euclidean' and so isn't specified in this case.
Of course, it could be even shorter if we allow the input to already be combined-together as a matrix, but this wouldn't be so neat...
R, 23 bytes
function(m)min(dist(m))
05AB1E, 8 bytes
œ€ü-nOtß
Commented:
œ # take all permutations of the input
€ # for each permutation:
- # take the element-wise difference
ü # between each pair of adjacent points
n # square each number
O # sum all difference-lists
t # take the square root of every sum
ß # take the minimum
Jelly, 9 bytes
ŒcZ_/²§Ṃ½
Works with points of any dimension
Explanation
ŒcZ_/²§Ṃ½ # Take as input a list of points, where each point is a list of coordinates
Œc # All pairs of two distinct points [(p1,p2),(p1,p3),...]
Z # Transpose to get two lists of points [[p1,p1,...],[p2,p3,...]]
_/ # Depth-1 vectorizing difference [p1-p2, p1-p3, ...]
²§ # Square coordinates and sum each
Ṃ # Minimum
½ # Square root