From e9b7c12523275537a7831783e247da0538047a2f Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Fri, 27 Dec 2024 18:31:45 +0500 Subject: [PATCH] Ruby: use x...y instead of x..y-1 --- advent-of-code/2024/day_06/lib.rb | 8 ++++---- advent-of-code/2024/day_08/lib.rb | 8 ++++---- advent-of-code/2024/day_10/lib.rb | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/advent-of-code/2024/day_06/lib.rb b/advent-of-code/2024/day_06/lib.rb index 449cfa2..ae431c9 100644 --- a/advent-of-code/2024/day_06/lib.rb +++ b/advent-of-code/2024/day_06/lib.rb @@ -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] = '#' diff --git a/advent-of-code/2024/day_08/lib.rb b/advent-of-code/2024/day_08/lib.rb index 58a21bf..073048a 100644 --- a/advent-of-code/2024/day_08/lib.rb +++ b/advent-of-code/2024/day_08/lib.rb @@ -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 == '.' diff --git a/advent-of-code/2024/day_10/lib.rb b/advent-of-code/2024/day_10/lib.rb index 869a71b..cf6492f 100644 --- a/advent-of-code/2024/day_10/lib.rb +++ b/advent-of-code/2024/day_10/lib.rb @@ -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)