Add solution to day 6.
This commit is contained in:
parent
fde761d6af
commit
b0e079de0f
65
day6.cr
Normal file
65
day6.cr
Normal file
|
@ -0,0 +1,65 @@
|
|||
require "./common.cr"
|
||||
|
||||
lines = File.read("day6_input").split("\n")
|
||||
lines.pop
|
||||
|
||||
REGEX = /(\d+), (\d+)/
|
||||
coords = lines.map(&.match(REGEX))
|
||||
.select { |it| it != nil }
|
||||
.map { |match| { match.not_nil![1].to_i32, match.not_nil![2].to_i32 } }
|
||||
|
||||
def distance(a, b)
|
||||
(a[0] - b[0]).abs + (a[1] - b[1]).abs
|
||||
end
|
||||
|
||||
def compute_area(coords, coord)
|
||||
visited = Set(Tuple(Int32, Int32)).new
|
||||
total = 0
|
||||
|
||||
coord_queue = Set(Tuple(Int32, Int32)).new
|
||||
coord_queue << coord
|
||||
|
||||
while !coord_queue.empty?
|
||||
new_coord = coord_queue.first
|
||||
coord_queue.delete new_coord
|
||||
|
||||
next if visited.includes? new_coord
|
||||
visited << new_coord
|
||||
|
||||
distances = coords.group_by do |coord|
|
||||
distance(coord, new_coord)
|
||||
end
|
||||
min_distance = distances.keys.min
|
||||
|
||||
next if distances[min_distance].size != 1
|
||||
next if distances[min_distance][0] != coord
|
||||
|
||||
total += 1
|
||||
x, y = new_coord
|
||||
coord_queue << { x + 1, y }
|
||||
coord_queue << { x - 1, y }
|
||||
coord_queue << { x, y + 1 }
|
||||
coord_queue << { x, y - 1 }
|
||||
|
||||
if total > 10000
|
||||
return 0
|
||||
end
|
||||
end
|
||||
|
||||
return total
|
||||
end
|
||||
|
||||
def find_region(coords)
|
||||
total = 0
|
||||
(-11000/2..11000/2).each do |x|
|
||||
(-11000/2..11000/2).each do |y|
|
||||
coord = {x, y}
|
||||
total += 1 if coords.sum { |it| distance(it, coord) } < 10000
|
||||
end
|
||||
end
|
||||
|
||||
return total
|
||||
end
|
||||
|
||||
puts coords.max_of { |it| compute_area(coords, it) }
|
||||
puts find_region(coords)
|
Loading…
Reference in New Issue
Block a user