Fix some rubocop warnings
This commit is contained in:
parent
4b711f4eb4
commit
0632c7752b
1 changed files with 68 additions and 73 deletions
|
@ -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 "^"
|
||||
when '^'
|
||||
if i == 0
|
||||
matrix[i][j] = "."
|
||||
matrix[i][j] = '.'
|
||||
return [false, -1, -1]
|
||||
end
|
||||
|
||||
if matrix[i-1][j] == "#"
|
||||
matrix[i][j] = ">"
|
||||
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"
|
||||
matrix[i - 1][j] = '^'
|
||||
matrix[i][j] = '.'
|
||||
[true, i - 1, j]
|
||||
when 'v'
|
||||
if i == matrix.length - 1
|
||||
matrix[i][j] = "."
|
||||
matrix[i][j] = '.'
|
||||
return [false, -1, -1]
|
||||
end
|
||||
|
||||
if matrix[i+1][j] == "#"
|
||||
matrix[i][j] = "<"
|
||||
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 ">"
|
||||
matrix[i + 1][j] = 'v'
|
||||
matrix[i][j] = '.'
|
||||
[true, i + 1, j]
|
||||
when '>'
|
||||
if j == matrix[i].length - 1
|
||||
matrix[i][j] = "."
|
||||
matrix[i][j] = '.'
|
||||
return [false, -1, -1]
|
||||
end
|
||||
|
||||
if matrix[i][j+1] == "#"
|
||||
matrix[i][j] = "v"
|
||||
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 "<"
|
||||
matrix[i][j + 1] = '>'
|
||||
matrix[i][j] = '.'
|
||||
[true, i, j + 1]
|
||||
when '<'
|
||||
if j == 0
|
||||
matrix[i][j] = "."
|
||||
matrix[i][j] = '.'
|
||||
return [false, -1, -1]
|
||||
end
|
||||
|
||||
if matrix[i][j-1] == "#"
|
||||
matrix[i][j] = "^"
|
||||
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
|
||||
return [i, j] if ['^', '>', 'v', '<'].include?(matrix[i][j])
|
||||
end
|
||||
end
|
||||
|
||||
return []
|
||||
[]
|
||||
end
|
||||
|
||||
def count_possible_obstructions(matrix)
|
||||
|
@ -97,33 +96,29 @@ def count_possible_obstructions(matrix)
|
|||
|
||||
for i in 0..matrix.length - 1 do
|
||||
for j in 0..matrix[i].length - 1 do
|
||||
if matrix[i][j] != "."
|
||||
next
|
||||
end
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue