original_docstring: Go through the grid and find the portals
docstring: Go through the grid and find the portals
docstring_tokens: Go through the grid and find the portals
code: def find_portals(self):
          """Go through the grid and find the portals"""
  
          # Figure out height and width of the maze
          h, w = len(self.grid), len(self.grid[0])
  
          # Use a dict to keep track of the portals
          portals = defaultdict(list)
  
          for y in range(h):
              for x in range(w):
  
                  # Check if the key is an upper case letter
                  key, key_pair = self.grid[y][x], ""
                  if not key.isupper():
                      continue
  
                  # Look up
                  if (
                      y - 2 > 0
                      and self.grid[y - 1][x].isupper()
                      and self.grid[y - 2][x] == "."
                  ):
                      yp, xp = y - 2, x
                      key_pair = self.grid[y - 1][x] + key
                  # Look down
                  elif (
                      y + 2 < h
                      and self.grid[y + 1][x].isupper()
                      and self.grid[y + 2][x] == "."
                  ):
                      yp, xp = y + 2, x
                      key_pair = key + self.grid[y + 1][x]
                  # Look left
                  elif (
                      x - 2 > 0
                      and self.grid[y][x - 1].isupper()
                      and self.grid[y][x - 2] == "."
                  ):
                      yp, xp = y, x - 2
                      key_pair = self.grid[y][x - 1] + key
                  # Look right
                  elif (
                      x + 2 < w
                      and self.grid[y][x + 1].isupper()
                      and self.grid[y][x + 2] == "."
                  ):
                      yp, xp = y, x + 2
                      key_pair = key + self.grid[y][x + 1]
  
                  # If a portal is found: append its position
                  if key_pair:
                      portals[key_pair].append((yp, xp))
  
          return portals, self.get_reachable_portals(portals, w)
code_tokens: def find_portals ( self ) : h , w = len ( self . grid ) , len ( self . grid [ 0 ] ) portals = defaultdict ( list ) for y in range ( h ) : for x in range ( w ) : key , key_pair = self . grid [ y ] [ x ] , "" if not key . isupper ( ) : continue if ( y - 2 > 0 and self . grid [ y - 1 ] [ x ] . isupper ( ) and self . grid [ y - 2 ] [ x ] == "." ) : yp , xp = y - 2 , x key_pair = self . grid [ y - 1 ] [ x ] + key elif ( y + 2 < h and self . grid [ y + 1 ] [ x ] . isupper ( ) and self . grid [ y + 2 ] [ x ] == "." ) : yp , xp = y + 2 , x key_pair = key + self . grid [ y + 1 ] [ x ] elif ( x - 2 > 0 and self . grid [ y ] [ x - 1 ] . isupper ( ) and self . grid [ y ] [ x - 2 ] == "." ) : yp , xp = y , x - 2 key_pair = self . grid [ y ] [ x - 1 ] + key elif ( x + 2 < w and self . grid [ y ] [ x + 1 ] . isupper ( ) and self . grid [ y ] [ x + 2 ] == "." ) : yp , xp = y , x + 2 key_pair = key + self . grid [ y ] [ x + 1 ] if key_pair : portals [ key_pair ] . append ( ( yp , xp ) ) return portals , self . get_reachable_portals ( portals , w )
short_docstring: Go through the grid and find the portals
short_docstring_tokens: Go through the grid and find the portals
comment: """Go through the grid and find the portals"""$SEP$# Figure out height and width of the maze$SEP$# Use a dict to keep track of the portals$SEP$# Check if the key is an upper case letter$SEP$# Look up$SEP$# Look down$SEP$# Look left$SEP$# Look right$SEP$# If a portal is found: append its position