Ruby: use x...y instead of x..y-1

This commit is contained in:
Ivan R. 2024-12-27 18:31:45 +05:00
parent 5cf8b5a565
commit e9b7c12523
Signed by: lumin
GPG key ID: E0937DC7CD6D3817
3 changed files with 10 additions and 10 deletions

View file

@ -79,8 +79,8 @@ def make_step(matrix, i, j)
end
def get_current_position(matrix)
for i in 0..matrix.length - 1 do
for j in 0..matrix[i].length - 1 do
for i in 0...matrix.length do
for j in 0...matrix[i].length do
return [i, j] if ['^', '>', 'v', '<'].include?(matrix[i][j])
end
end
@ -94,8 +94,8 @@ def count_possible_obstructions(matrix)
start_i, start_j = get_current_position(matrix)
start_symbol = matrix[start_i][start_j]
for i in 0..matrix.length - 1 do
for j in 0..matrix[i].length - 1 do
for i in 0...matrix.length do
for j in 0...matrix[i].length do
next if matrix[i][j] != '.'
matrix[i][j] = '#'

View file

@ -14,8 +14,8 @@ def count_antinodes(matrix, limit_length)
antinodes = Array.new(matrix.length) { Array.new(matrix[0].length, 0) }
station_coords.each_value do |loc|
(0..loc.length - 2).each do |i|
(i + 1..loc.length - 1).each do |j|
(0...loc.length - 1).each do |i|
(i + 1...loc.length).each do |j|
x_diff = loc[j].x - loc[i].x
y_diff = loc[j].y - loc[i].y
@ -69,8 +69,8 @@ end
def extract_stations(matrix)
station_coords = {}
(0..matrix.length - 1).each do |x|
(0..matrix[x].length - 1).each do |y|
(0...matrix.length).each do |x|
(0...matrix[x].length).each do |y|
ch = matrix[x][y]
next if ch == '.'

View file

@ -9,8 +9,8 @@ end
def get_score_sum(matrix, skip_visited_cells)
count = 0
for i in 0..matrix.length - 1 do
for j in 0..matrix[0].length - 1 do
for i in 0...matrix.length do
for j in 0...matrix[0].length do
next if matrix[i][j] != 0
count += bfs(matrix, i, j, skip_visited_cells)