Class: AocUtils::MazeUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/aoc_utils.rb

Overview

provides methods for working with a maze. The maze is represented as a 2D array of characters where ‘#’ represents a wall and ‘.’ represents an empty space

Class Method Summary collapse

Class Method Details

.calculate_distance_to_point(maze, point) ⇒ Object



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

.find_char(maze) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/aoc_utils.rb', line 76

def self.find_char(maze)
  maze.each_with_index do |row, y|
    row.each_with_index do |cell, x|
      return [x, y] if yield(cell)
    end
  end
end