From bbc0b34240f285bd122dc8cccea5f61cf0d1e375 Mon Sep 17 00:00:00 2001 From: Slavunderkind Date: Wed, 11 Jan 2017 16:19:24 +0200 Subject: [PATCH 1/2] Fix type error - defibrillator instead of defibrilator --- Training/Easy/defibrillators.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Training/Easy/defibrillators.rb b/Training/Easy/defibrillators.rb index 753775e..5619d47 100644 --- a/Training/Easy/defibrillators.rb +++ b/Training/Easy/defibrillators.rb @@ -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, :n, :defibrillators def initialize @lon = parse(gets.chomp) @lat = parse(gets.chomp) @n = gets.to_i - @defibrilators = {} + @defibrillators = {} end def calculate_x(d_long, d_lat) @@ -26,11 +26,11 @@ def parse(var) (var.sub(',', '.').to_f * Math::PI) / 180 end - def find_closest(defibrilators_hash) - min = defibrilators_hash.values.first - closest_name = defibrilators_hash.keys.first + def find_closest(defibrillators_hash) + min = defibrillators_hash.values.first + closest_name = defibrillators_hash.keys.first - defibrilators_hash.map do |key, value| + defibrillators_hash.map do |key, value| if value < min min = value closest_name = key @@ -39,18 +39,18 @@ def find_closest(defibrilators_hash) closest_name end - def init_defibrilators(defibrilator) - d_long = parse(defibrilator[4]) - d_lat = parse(defibrilator[5]) + def init_defibrillators(defibrillator) + d_long = parse(defibrillator[4]) + d_lat = parse(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) end def start n.times do defib = gets.chomp - init_defibrilators(defib.split(';')) + init_defibrillators(defib.split(';')) end puts find_closest(defibrillators) end From 23f345091286cddf9976ec2c14dca6eba8d5caa6 Mon Sep 17 00:00:00 2001 From: Slavunderkind Date: Fri, 20 Jan 2017 17:13:41 +0200 Subject: [PATCH 2/2] Simplify the code and remove unnecessary function find_closest --- Training/Easy/defibrillators.rb | 40 ++++++++++++--------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/Training/Easy/defibrillators.rb b/Training/Easy/defibrillators.rb index 5619d47..7b38852 100644 --- a/Training/Easy/defibrillators.rb +++ b/Training/Easy/defibrillators.rb @@ -1,11 +1,11 @@ # Solution for https://www.codingame.com/ide/puzzle/defibrillators class Defibrillators - attr_accessor :lon, :lat, :n, :defibrillators + attr_accessor :lon, :lat, :defibrilators_count, :defibrillators def initialize - @lon = parse(gets.chomp) - @lat = parse(gets.chomp) - @n = gets.to_i + @lon = degrees_to_radians(gets.chomp) + @lat = degrees_to_radians(gets.chomp) + @defibrilators_count = gets.to_i @defibrillators = {} end @@ -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(defibrillators_hash) - min = defibrillators_hash.values.first - closest_name = defibrillators_hash.keys.first - - defibrillators_hash.map do |key, value| - if value < min - min = value - closest_name = key - end - end - closest_name - end - - def init_defibrillators(defibrillator) - d_long = parse(defibrillator[4]) - d_lat = parse(defibrillator[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) defibrillators[defibrillator[1]] = distance(x, y) end def start - n.times do + Array.new(defibrilators_count) do defib = gets.chomp - init_defibrillators(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