Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 16 additions & 28 deletions Training/Easy/defibrillators.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Solution for https://www.codingame.com/ide/puzzle/defibrillators
class Defibrillators
attr_accessor :lon, :lat, :n, :defibrilators
attr_accessor :lon, :lat, :defibrilators_count, :defibrillators

def initialize
@lon = parse(gets.chomp)
@lat = parse(gets.chomp)
@n = gets.to_i
@defibrilators = {}
@lon = degrees_to_radians(gets.chomp)
@lat = degrees_to_radians(gets.chomp)
@defibrilators_count = gets.to_i
@defibrillators = {}
end

def calculate_x(d_long, d_lat)
Expand All @@ -22,39 +22,27 @@ def distance(x, y)
Math.sqrt(x * x + y * y) * 6371
end

def parse(var)
(var.sub(',', '.').to_f * Math::PI) / 180
def degrees_to_radians(degrees)
(degrees.sub(',', '.').to_f * Math::PI) / 180
end

def find_closest(defibrilators_hash)
min = defibrilators_hash.values.first
closest_name = defibrilators_hash.keys.first

defibrilators_hash.map do |key, value|
if value < min
min = value
closest_name = key
end
end
closest_name
end

def init_defibrilators(defibrilator)
d_long = parse(defibrilator[4])
d_lat = parse(defibrilator[5])
def add_defibrillator(defibrillator)
d_long = degrees_to_radians(defibrillator[4])
d_lat = degrees_to_radians(defibrillator[5])
x = calculate_x(d_long, d_lat)
y = calculate_y(d_lat)
defibrilators[defibrilator[1]] = distance(x, y)
defibrillators[defibrillator[1]] = distance(x, y)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inside the distance function, you can use Ruby's "power" operator - so instead of

x * x

you can do

x ** 2

end

def start
n.times do
Array.new(defibrilators_count) do
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evgenispasov-which special for you

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defib = gets.chomp
init_defibrilators(defib.split(';'))
add_defibrillator(defib.split(';'))
end
puts find_closest(defibrillators)
defibrillators.min_by { |_, v| v }.first
end
end

obj = Defibrillators.new
obj.start
answer = obj.start
puts answer