From 0632c7752b5f050d78c089e06757ab02b3b53b65 Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Mon, 9 Dec 2024 12:46:52 +0500 Subject: [PATCH] Fix some rubocop warnings --- advent-of-code/2024/day_06/lib.rb | 141 ++++++++++++++---------------- 1 file changed, 68 insertions(+), 73 deletions(-) diff --git a/advent-of-code/2024/day_06/lib.rb b/advent-of-code/2024/day_06/lib.rb index 2ba00b1..449cfa2 100644 --- a/advent-of-code/2024/day_06/lib.rb +++ b/advent-of-code/2024/day_06/lib.rb @@ -2,91 +2,90 @@ def walk(matrix) should_continue = true i, j = get_current_position(matrix) - start_i, start_j = i, j + start_i = i + start_j = j start_symbol = matrix[i][j] - history = Hash.new + history = {} - while should_continue do + while should_continue history["#{i},#{j}"] = true should_continue, i, j = make_step(matrix, i, j) end matrix[start_i][start_j] = start_symbol - return history.length + history.length end def make_step(matrix, i, j) case matrix[i][j] - when "^" - if i == 0 - matrix[i][j] = "." - return [false, -1, -1] - end + when '^' + if i == 0 + matrix[i][j] = '.' + return [false, -1, -1] + end - if matrix[i-1][j] == "#" - matrix[i][j] = ">" - return [true, i, j] - end + if matrix[i - 1][j] == '#' + matrix[i][j] = '>' + return [true, i, j] + end - matrix[i-1][j] = "^" - matrix[i][j] = "." - return [true, i-1, j] - when "v" - if i == matrix.length-1 - matrix[i][j] = "." - return [false, -1, -1] - end + matrix[i - 1][j] = '^' + matrix[i][j] = '.' + [true, i - 1, j] + when 'v' + if i == matrix.length - 1 + matrix[i][j] = '.' + return [false, -1, -1] + end - if matrix[i+1][j] == "#" - matrix[i][j] = "<" - return [true, i, j] - end + if matrix[i + 1][j] == '#' + matrix[i][j] = '<' + return [true, i, j] + end - matrix[i+1][j] = "v" - matrix[i][j] = "." - return [true, i+1, j] - when ">" - if j == matrix[i].length-1 - matrix[i][j] = "." - return [false, -1, -1] - end + matrix[i + 1][j] = 'v' + matrix[i][j] = '.' + [true, i + 1, j] + when '>' + if j == matrix[i].length - 1 + matrix[i][j] = '.' + return [false, -1, -1] + end - if matrix[i][j+1] == "#" - matrix[i][j] = "v" - return [true, i, j] - end + if matrix[i][j + 1] == '#' + matrix[i][j] = 'v' + return [true, i, j] + end - matrix[i][j+1] = ">" - matrix[i][j] = "." - return [true, i, j+1] - when "<" - if j == 0 - matrix[i][j] = "." - return [false, -1, -1] - end + matrix[i][j + 1] = '>' + matrix[i][j] = '.' + [true, i, j + 1] + when '<' + if j == 0 + matrix[i][j] = '.' + return [false, -1, -1] + end - if matrix[i][j-1] == "#" - matrix[i][j] = "^" - return [true, i, j] - end + if matrix[i][j - 1] == '#' + matrix[i][j] = '^' + return [true, i, j] + end - matrix[i][j-1] = "<" - matrix[i][j] = "." - return [true, i, j-1] + matrix[i][j - 1] = '<' + matrix[i][j] = '.' + [true, i, j - 1] end end def get_current_position(matrix) - for i in 0..matrix.length-1 do - for j in 0..matrix[i].length-1 do - if ['^', '>', 'v', '<'].include?(matrix[i][j]) - return [i, j] - end + for i in 0..matrix.length - 1 do + for j in 0..matrix[i].length - 1 do + return [i, j] if ['^', '>', 'v', '<'].include?(matrix[i][j]) end end - return [] + [] end def count_possible_obstructions(matrix) @@ -95,35 +94,31 @@ 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 - if matrix[i][j] != "." - next - end + for i in 0..matrix.length - 1 do + for j in 0..matrix[i].length - 1 do + next if matrix[i][j] != '.' - matrix[i][j] = "#" - if test_loop(matrix, start_i, start_j) - count += 1 - end - matrix[i][j] = "." + matrix[i][j] = '#' + count += 1 if test_loop(matrix, start_i, start_j) + matrix[i][j] = '.' matrix[start_i][start_j] = start_symbol end end - return count + count end def test_loop(matrix, i, j) should_continue = true - history = Hash.new + history = {} - while should_continue do + while should_continue symbol = matrix[i][j] key = "#{i},#{j},#{symbol}" if history.include?(key) - matrix[i][j] = "." + matrix[i][j] = '.' return true end @@ -132,5 +127,5 @@ def test_loop(matrix, i, j) should_continue, i, j = make_step(matrix, i, j) end - return false + false end