Skip to content

Commit

Permalink
2021-15: Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
bcc32 committed Dec 29, 2023
1 parent 382b0b7 commit ec1c2c5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
9 changes: 6 additions & 3 deletions 2021/15/sol1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ def dijk(grid, start, end_)
until queue.empty?
x, y = queue.min_by(&dist)
d = dist[[x, y]]
return d if [x, y] == end_
return d if end_ == [x, y]

queue.delete([x, y])
[[-1, 0], [1, 0], [0, -1], [0, 1]].each do |dx, dy|
next unless (0...grid.size) === x + dx && (0...grid[0].size) === y + dy
next if dist.has_key?([x + dx, y + dy])
next unless (0...grid.size).include?(x + dx) && (0...grid[0].size).include?(y + dy)

next if dist.key?([x + dx, y + dy])

cost = grid[x + dx] && grid[x + dx][y + dy]
dist[[x + dx, y + dy]] = d + cost
queue << [x + dx, y + dy]
Expand Down
11 changes: 7 additions & 4 deletions 2021/15/sol2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def cost(grid, x, y)
ym = y % grid[0].size
ans = grid[xm][ym] + xx + yy
ans = ans % 9
ans = 9 if ans == 0
ans = 9 if ans.zero?
ans
end

Expand All @@ -29,10 +29,13 @@ def dijk(grid, xmax, ymax, start, end_)
until pqueue.empty?
x, y = pqueue.pop
d = dist[[x, y]]
return d if [x, y] == end_
return d if end_ == [x, y]

[[-1, 0], [1, 0], [0, -1], [0, 1]].each do |dx, dy|
next unless (0..xmax) === x + dx && (0..ymax) === y + dy
next if dist.has_key?([x + dx, y + dy])
next unless (0..xmax).include?(x + dx) && (0..ymax).include?(y + dy)

next if dist.key?([x + dx, y + dy])

cost = cost(grid, x + dx, y + dy)
dist[[x + dx, y + dy]] = d + cost
pqueue.push(d + cost, [x + dx, y + dy])
Expand Down

0 comments on commit ec1c2c5

Please sign in to comment.