84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/aoc_utils.rb', line 84
def self.calculate_distance_to_point(maze, point)
maze[point[1]][point[0]] = 0
queue = [point]
until queue.empty?
current = queue.shift
value = maze[current[1]][current[0]]
[[-1, 0], [1, 0], [0, -1], [0, 1]].each do |direction|
new_coordinate = [current[0] + direction[0], current[1] + direction[1]]
if new_coordinate[0] < 0 || new_coordinate[0] >= maze[0].length || new_coordinate[1] < 0 || new_coordinate[1] >= maze.length
next
end
if maze[new_coordinate[1]][new_coordinate[0]] == "."
maze[new_coordinate[1]][new_coordinate[0]] = value + 1
queue.push(new_coordinate)
end
end
end
end
|