読者です 読者をやめる 読者になる 読者になる

CALCULATE DISTANCE(CodeEval)

codeeval ruby

CHALLENGE DESCRIPTION:

You have coordinates of 2 points and need to find the distance between them.

INPUT SAMPLE:

Your program should accept as its first argument a path to a filename. Input example is the following

(25, 4) (1, -6)
(47, 43) (-25, -11)

OUTPUT SAMPLE:

Print results in the following way.

26
90

You don't need to round the results you receive. They must be integer numbers between -100 and 100.

My Code

#!/usr/bin/env ruby -w

def calculate_distance(x1, y1, x2, y2)
  Math.sqrt((x1 - x2)**2 + (y1 - y2)**2).to_i
end

ARGF.each_line do |line|
  x1, y1, x2, y2 = line.chomp.scan(/-?\d+/).map(&:to_i)
  puts calculate_distance(x1, y1, x2, y2)
end