Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.30 KB | None | 0 0
  1.  
  2. def gen_taboos(warehouse):
  3.  
  4.     taboo = []
  5.    
  6.     #go along the walls and find corners
  7.     for (x,y) in warehouse.walls:
  8.         #if we have a gap on the right, ( left wall ) and a wall above or below that:
  9.         if (x+1,y) not in warehouse.walls and (
  10.         (x+1,y+1) in warehouse.walls or (x+1,y-1) in warehouse.walls):
  11.             taboo.append((x+1,y))
  12.            
  13.         #and for the gap on the left ( right wall ) yadda yadda
  14.         if(x-1,y) not in warehouse.walls and (
  15.         (x-1,y+1) in warehouse.walls or (x-1,y-1) in warehouse.walls):
  16.             taboo.append((x-1,y))
  17.  
  18.  
  19.     #remove any taboos that are targets
  20.     taboo = [t for t in taboo if t not in warehouse.targets]
  21.    
  22.    
  23.     #check if outside play area
  24.     botmost = max(warehouse.walls, key=lambda x:x[1])[1] + 1
  25.     rigmost = max(warehouse.walls, key=lambda x:x[0])[0] + 1
  26.    
  27.     #for each taboo, raycast to the top/bot/right/leftmost walls
  28.     removal = []
  29.     for t in taboo:
  30.         cx,cy = t[0],t[1]
  31.         raycast = []        #raycast ( direction, target )
  32.         vert_ray = True
  33.         hori_ray = True
  34.  
  35.         if(cx,cy+1) in warehouse.walls: # wall is below us
  36.             raycast = (-1,0)
  37.         else:
  38.             raycast = (1,botmost)
  39.            
  40.         for i in range(cy,raycast[1],raycast[0]):
  41.             if(cx,i) in warehouse.walls:
  42.                 vert_ray = False
  43.                 break
  44.            
  45.         if(cx+1,cy) in warehouse.walls: #wall is to the right
  46.             raycast = (-1,0)
  47.         else:
  48.             raycast = (1,rigmost)
  49.            
  50.         for i in range(cx,raycast[1],raycast[0]):
  51.             if(i,cy) in warehouse.walls:
  52.                 hori_ray = False
  53.                 break
  54.        
  55.         if hori_ray or vert_ray:
  56.             removal.append(t) #see note below
  57.        
  58.     #remove all that are marked for removal
  59.     #note: could not do in loop due to loop index skipping over dead elements.
  60.    
  61.     taboo = [t for t in taboo if t not in removal]
  62.  
  63.  
  64.     taboo_pairs = []
  65.     for t in taboo:
  66.         for T in taboo:
  67.             if (T[0] == t[0] or t[1] == T[1]) and T is not t:
  68.                 if [T,t] not in taboo_pairs or [t,T] not in taboo_pairs:  
  69.                     taboo_pairs.append([t,T])
  70.  
  71.     for pair in taboo_pairs:
  72.        
  73.         wall_check = 0      #wall runner
  74.         gap = False     #a gap was found ( not taboo )
  75.         a,b = pair[0],pair[1]   #each taboo
  76.        
  77.         if(a[0] == b[0]): #x components are the same, so check along y
  78.             # (2,2) -> (2,6)
  79.            
  80.             if(a[0]+1,a[1]) in warehouse.walls: #check which side wall is on
  81.                 wall_check=a[0]+1
  82.             else:
  83.                 wall_check=a[0]-1
  84.        
  85.             #run along wall, see if there's a gap
  86.             for i in range(a[1],b[1]):
  87.                 if (wall_check,i) not in warehouse.walls or (a[0],i) in warehouse.walls or (a[0],i) in warehouse.targets:
  88.                     gap = True
  89.                    
  90.             #if we didn't have a gap, paint the entire wall
  91.             if not gap:
  92.                 for i in range(a[1],b[1]):
  93.                     taboo.append((a[0],i))
  94.  
  95.            
  96.         else: #check along x direction
  97.             if(a[0],a[1]+1) in warehouse.walls: #check which side wall is on
  98.                 wall_check=a[1]+1
  99.             else: #it was on the other side
  100.                 wall_check=a[1]-1
  101.        
  102.             for i in range(a[0],b[0]):
  103.                 if (i,wall_check) not in warehouse.walls or (i,a[1]) in warehouse.walls or (i,a[1]) in warehouse.targets:
  104.                     gap = True
  105.        
  106.             if not gap:
  107.                 for i in range(a[0],b[0]):
  108.                     taboo.append((i,a[1]))
  109.                    
  110.                    
  111.                    
  112.     #return the entire list of taboo tuples
  113.     return taboo
  114.    
  115.  
  116. def taboo_cells(warehouse):
  117.     '''  
  118.    Identify the taboo cells of a warehouse. A cell is called 'taboo'
  119.    if whenever a box get pushed on such a cell then the puzzle becomes unsolvable.  
  120.    When determining the taboo cells, you must ignore all the existing boxes,
  121.    simply consider the walls and the target  cells.  
  122.    Use only the following two rules to determine the taboo cells;
  123.     Rule 1: if a cell is a corner and not a target, then it is a taboo cell.
  124.     Rule 2: all the cells between two corners along a wall are taboo if none of
  125.             these cells is a target.
  126.  
  127.    @param warehouse: a Warehouse object
  128.  
  129.    @return
  130.       A string representing the puzzle with only the wall cells marked with
  131.       an '#' and the taboo cells marked with an 'X'.  
  132.       The returned string should NOT have marks for the worker, the targets,
  133.       and the boxes.  
  134.    '''
  135.    
  136.     taboo = gen_taboos(warehouse)
  137.    
  138.     #zip it up
  139.     #shamelessly copied from example code
  140.     X,Y = zip(*warehouse.walls)
  141.     x_size, y_size = 1+max(X), 1+max(Y)
  142.     vis = [[" "] * x_size for y in range(y_size)]
  143.     for (x,y) in warehouse.walls:
  144.         vis[y][x] = "#"
  145.     for (x,y) in taboo:
  146.         vis[y][x] = "X"
  147.     return "\n".join(["".join(line) for line in vis])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement