def calc(p1, p2)
  x = p1[0]-p2[0]
  y = p1[1]-p2[1]
  Math.sqrt(x*x+y*y)
end

def permutation(a, x, xx)
  if a.size == 0
    return xx << x
  end

  a.each.with_index do |c, i|
    b = a.dup
    b.delete_at(i)
    permutation(b, x + [c], xx)
  end
end

def factr(n)
  (1..n).reduce(&:*)
end

a = [
  [-406, 10],
  [512, 859],
  [494, 362],
  [-955, -475],
  [128, 553],
  [-986, -885],
  [763, 77],
  [449, 310],
]
f = factr(a.size)

xx = []
permutation(a, [], xx)

total = 0
xx.each do |x|
  sum = 0
  x.each_cons(2) do |a, b|
    sum += calc(a, b)
  end
  total += sum / f
end

puts total