Created
May 4, 2020 17:20
-
-
Save justinhj/dad18f16899f7ccc5d4bbac11afcfa7e to your computer and use it in GitHub Desktop.
Question a certain large company asked me to do in 15 minutes lol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry-byebug' | |
Star = Struct.new(:x, :y, :name, :constellation, :adjacents) | |
def calc_constellations(stars) | |
# Generate for each star, a list of adjacent stars | |
stars.each {|s| | |
s.adjacents = [] | |
stars.each {|s2| | |
if s.x == s2.x && (s.y - s2.y).abs == 1 || s.y == s2.y && (s.x - s2.x).abs == 1 | |
s.adjacents.push(s2) | |
end | |
} | |
} | |
c_id = 1 | |
# Generate the connected components | |
stars.each {|s| | |
connected_star = s.adjacents.find{|s1| !s1.constellation.nil?} | |
if !connected_star.nil? | |
s.constellation = connected_star.constellation | |
else | |
s.constellation = c_id | |
c_id += 1 | |
end | |
} | |
end | |
stars = [Star.new(10,10,"Jamie"), | |
Star.new(11,10,"Ross"), | |
Star.new(12,10,"Nero"), | |
Star.new(14,10,"Lisa"), | |
Star.new(14,11,"Bob")] | |
calc_constellations(stars) | |
def get_stars(x,y, stars) | |
star = stars.find{|s| s.x == x && s.y == y} | |
if !star.nil? | |
stars.each{|s| | |
if s.constellation == star.constellation | |
puts s.name | |
end | |
} | |
end | |
end | |
get_stars(11,10,stars) | |
puts | |
get_stars(14,11,stars) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment