Skip to content

Instantly share code, notes, and snippets.

@dengsauve
Last active May 13, 2017 10:02
Show Gist options
  • Save dengsauve/a83693bd982ef1c362c3c06af4458ceb to your computer and use it in GitHub Desktop.
Save dengsauve/a83693bd982ef1c362c3c06af4458ceb to your computer and use it in GitHub Desktop.
/r/dailyprogrammer Chalenge #314 [Hard] Finding Point Nemo created by dengsauve - https://repl.it/HwK9/6
require_relative('sea_cell')
fin = File.open('map.txt', 'r')
dims = fin.readline.strip.split(' ')
map = fin.readlines
fin.close
world = Nemo.new(map, dims)
sea_cells = []
map.each_with_index do |latitude, lat|
latitude.split('').each_with_index do |cell, long|
if cell == ' '
sea_cells << SeaCell.new(lat, long)
end
end
end
measure = 0.0
point_nemo = SeaCell
sea_cells.each do |cell|
if cell.land > measure
measure = cell.land
point_nemo = cell
end
end
puts point_nemo.to_s
world.x_marks_the_spot(point_nemo.lat, point_nemo.long)
puts world.map
80 25
## # # # # # # ## ###
#### ###### ######## ###### ##### ######### #### #######
########## ## ##### #### # #####################
####################### ## ### ## #### #### ##
######### ######### ### ## # ### ## ##
# # ##### ####### ### # #
# ### ## #######
# ### ########### #
### ## ############## #
# ### ############## #
## #############
##### ########### ##
######### ########## ##
############ ######### ##
############### #######
############## ##### #########
############### ## ### ###########
############### # ############
############ ### ####
######### #
# #####
######## ###### #######
###################### ########################### ##############
##############################################################################
class Nemo
def initialize(map, dims)
@@map = map
@@width = dims[1].to_i - 1
@@height = dims[0].to_i - 1
@@max_long = dims[1].to_i / 2
@@max_lat = dims[0].to_i / 2
end
def x_marks_the_spot(lat, long)
@@map[lat][long] = 'X'
end
def map
return @@map
end
end
class SeaCell < Nemo
attr_reader :land, :lat, :long
def initialize(lat, long)
@lat = lat
@long = long
@land = land_ho
end
def land_ho
shortest = @@map.length*1.0
@@map.each_with_index do | latitude, y |
latitude.split('').each_with_index do | cell, x |
if cell == '#'
a = @lat - y > @@max_lat ? @@width - y : @lat - y
b = @long - x > @@max_long ? @@height - x : @long - x
c = Math.sqrt(a**2 + b**2)
shortest = c if c < shortest
end
end
end
return shortest
end
def to_s
return "#{@lat} x #{@long} units: #{@land}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment