You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
advent-of-code/2018/ruby/day_06.rb

46 lines
1.1 KiB

require "set"
coords = ARGF.read.chomp.lines.map(&:chomp).map {|line| line.split(", ").map(&:to_i) }
x_min = coords.map(&:first).min
x_max = coords.map(&:first).max
y_min = coords.map(&:last).min
y_max = coords.map(&:last).max
coords = coords.map.with_index {|c,i| [i, c] }
p coords
p x_min, x_max, y_min, y_max
def manhattan_distance(a, b)
(a[0] - b[0]).abs + (a[1] - b[1]).abs
end
grid = {}
(x_min..x_max).each do |x|
(y_min..y_max).each do |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 }
grid[[x,y]] = closests.length > 1 ? nil : closests.first.first
end
end
infinites = Set.new
(x_min..x_max).each do |x|
infinites << grid[[x,y_min]]
infinites << grid[[x,y_max]]
end
(y_min..y_max).each do |y|
infinites << grid[[x_min,y]]
infinites << grid[[x_max,y]]
end
p grid
p infinites
p grid.values.group_by(&:itself).transform_values(&:count).reject {|k,_| infinites.include?(k) }.max_by(&:last)