Fix some rubocop warnings

This commit is contained in:
Ivan R. 2024-12-09 12:46:52 +05:00
parent 4b711f4eb4
commit 0632c7752b
Signed by: lumin
GPG key ID: E0937DC7CD6D3817

View file

@ -2,91 +2,90 @@ def walk(matrix)
should_continue = true should_continue = true
i, j = get_current_position(matrix) i, j = get_current_position(matrix)
start_i, start_j = i, j start_i = i
start_j = j
start_symbol = matrix[i][j] start_symbol = matrix[i][j]
history = Hash.new history = {}
while should_continue do while should_continue
history["#{i},#{j}"] = true history["#{i},#{j}"] = true
should_continue, i, j = make_step(matrix, i, j) should_continue, i, j = make_step(matrix, i, j)
end end
matrix[start_i][start_j] = start_symbol matrix[start_i][start_j] = start_symbol
return history.length history.length
end end
def make_step(matrix, i, j) def make_step(matrix, i, j)
case matrix[i][j] case matrix[i][j]
when "^" when '^'
if i == 0 if i == 0
matrix[i][j] = "." matrix[i][j] = '.'
return [false, -1, -1] return [false, -1, -1]
end end
if matrix[i-1][j] == "#" if matrix[i - 1][j] == '#'
matrix[i][j] = ">" matrix[i][j] = '>'
return [true, i, j] return [true, i, j]
end end
matrix[i-1][j] = "^" matrix[i - 1][j] = '^'
matrix[i][j] = "." matrix[i][j] = '.'
return [true, i-1, j] [true, i - 1, j]
when "v" when 'v'
if i == matrix.length-1 if i == matrix.length - 1
matrix[i][j] = "." matrix[i][j] = '.'
return [false, -1, -1] return [false, -1, -1]
end end
if matrix[i+1][j] == "#" if matrix[i + 1][j] == '#'
matrix[i][j] = "<" matrix[i][j] = '<'
return [true, i, j] return [true, i, j]
end end
matrix[i+1][j] = "v" matrix[i + 1][j] = 'v'
matrix[i][j] = "." matrix[i][j] = '.'
return [true, i+1, j] [true, i + 1, j]
when ">" when '>'
if j == matrix[i].length-1 if j == matrix[i].length - 1
matrix[i][j] = "." matrix[i][j] = '.'
return [false, -1, -1] return [false, -1, -1]
end end
if matrix[i][j+1] == "#" if matrix[i][j + 1] == '#'
matrix[i][j] = "v" matrix[i][j] = 'v'
return [true, i, j] return [true, i, j]
end end
matrix[i][j+1] = ">" matrix[i][j + 1] = '>'
matrix[i][j] = "." matrix[i][j] = '.'
return [true, i, j+1] [true, i, j + 1]
when "<" when '<'
if j == 0 if j == 0
matrix[i][j] = "." matrix[i][j] = '.'
return [false, -1, -1] return [false, -1, -1]
end end
if matrix[i][j-1] == "#" if matrix[i][j - 1] == '#'
matrix[i][j] = "^" matrix[i][j] = '^'
return [true, i, j] return [true, i, j]
end end
matrix[i][j-1] = "<" matrix[i][j - 1] = '<'
matrix[i][j] = "." matrix[i][j] = '.'
return [true, i, j-1] [true, i, j - 1]
end end
end end
def get_current_position(matrix) def get_current_position(matrix)
for i in 0..matrix.length-1 do for i in 0..matrix.length - 1 do
for j in 0..matrix[i].length-1 do for j in 0..matrix[i].length - 1 do
if ['^', '>', 'v', '<'].include?(matrix[i][j]) return [i, j] if ['^', '>', 'v', '<'].include?(matrix[i][j])
return [i, j]
end
end end
end end
return [] []
end end
def count_possible_obstructions(matrix) def count_possible_obstructions(matrix)
@ -95,35 +94,31 @@ def count_possible_obstructions(matrix)
start_i, start_j = get_current_position(matrix) start_i, start_j = get_current_position(matrix)
start_symbol = matrix[start_i][start_j] start_symbol = matrix[start_i][start_j]
for i in 0..matrix.length-1 do for i in 0..matrix.length - 1 do
for j in 0..matrix[i].length-1 do for j in 0..matrix[i].length - 1 do
if matrix[i][j] != "." next if matrix[i][j] != '.'
next
end
matrix[i][j] = "#" matrix[i][j] = '#'
if test_loop(matrix, start_i, start_j) count += 1 if test_loop(matrix, start_i, start_j)
count += 1 matrix[i][j] = '.'
end
matrix[i][j] = "."
matrix[start_i][start_j] = start_symbol matrix[start_i][start_j] = start_symbol
end end
end end
return count count
end end
def test_loop(matrix, i, j) def test_loop(matrix, i, j)
should_continue = true should_continue = true
history = Hash.new history = {}
while should_continue do while should_continue
symbol = matrix[i][j] symbol = matrix[i][j]
key = "#{i},#{j},#{symbol}" key = "#{i},#{j},#{symbol}"
if history.include?(key) if history.include?(key)
matrix[i][j] = "." matrix[i][j] = '.'
return true return true
end end
@ -132,5 +127,5 @@ def test_loop(matrix, i, j)
should_continue, i, j = make_step(matrix, i, j) should_continue, i, j = make_step(matrix, i, j)
end end
return false false
end end