Advertisement
Nickpips

bits

Mar 21st, 2016 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. def read_file(path):
  4.     with open(path, 'rb') as f:
  5.         return f.read()
  6.  
  7. rawgrid = (' '.join(format(ord(x), '08b') for x in read_file("map.dat"))).replace(" ","")
  8.  
  9. grid = [[False for x in range(4802)] for x in range(4802)]
  10.    
  11. for i in range(0,4800):
  12.     for j in range(0,4800):
  13.         if rawgrid[i*4800+j] == '0':
  14.             grid[i+1][j+1] = True
  15.  
  16. count = 0
  17. queue = [(1,1)]
  18. while len(queue) != 0:
  19.     nqueue = []
  20.     while len(queue) != 0:
  21.         coord = queue.pop()
  22.        
  23.         x = coord[0]
  24.         y = coord[1]
  25.        
  26.         if grid[x][y] == False:
  27.             continue
  28.        
  29.         count += 1
  30.         grid[x][y] = False
  31.        
  32.         if grid[x-1][y] == True:
  33.             nqueue.append((x-1,y))
  34.         if grid[x+1][y] == True:
  35.             nqueue.append((x+1,y))
  36.         if grid[x][y-1] == True:
  37.             nqueue.append((x,y-1))
  38.         if grid[x][y+1] == True:
  39.             nqueue.append((x,y+1))
  40.     queue = nqueue
  41.    
  42. print count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement