[2018][ruby][06.1]

sorbet
Alpha Chen 6 years ago
parent b69bfd2821
commit 5995c5d54d

@ -20,26 +20,30 @@ end
grid = {} grid = {}
(x_min..x_max).each do |x| (x_min..x_max).each do |x|
(y_min..y_max).each do |y| (y_min..y_max).each do |y|
distances = coords.map {|id, coord| # Part One
[id, manhattan_distance(coord, [x,y])] # distances = coords.map {|id, coord|
} # [id, manhattan_distance(coord, [x,y])]
min_distance = distances.map(&:last).min # }
closests = distances.select {|_,distance| distance == min_distance } # min_distance = distances.map(&:last).min
grid[[x,y]] = closests.length > 1 ? nil : closests.first.first # closests = distances.select {|_,distance| distance == min_distance }
# grid[[x,y]] = closests.length > 1 ? nil : closests.first.first
# Part Two
total_distance = coords.map {|_,coord| manhattan_distance(coord, [x,y]) }.sum
grid[[x,y]] = true if total_distance < 10000
end end
end end
infinites = Set.new # Part One
(x_min..x_max).each do |x| # infinites = Set.new
infinites << grid[[x,y_min]] # (x_min..x_max).each do |x|
infinites << grid[[x,y_max]] # infinites << grid[[x,y_min]]
end # infinites << grid[[x,y_max]]
(y_min..y_max).each do |y| # end
infinites << grid[[x_min,y]] # (y_min..y_max).each do |y|
infinites << grid[[x_max,y]] # infinites << grid[[x_min,y]]
end # infinites << grid[[x_max,y]]
# end
p grid # p grid.values.group_by(&:itself).transform_values(&:count).reject {|k,_| infinites.include?(k) }.max_by(&:last)
p infinites p grid.size
p grid.values.group_by(&:itself).transform_values(&:count).reject {|k,_| infinites.include?(k) }.max_by(&:last)

Loading…
Cancel
Save