Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- grid = [[54,12 , 0, 0],
- [0, 39, 0, 30],
- [33,27,24, 42],
- [18, 0, 0, 0 ]]
- w = len(grid) #height or width
- #instructions to dynamically generate rows, columns, diagonals coords
- dirs = {i:[(w,1)[not i%w]] for i in range(w*w) if i<w or i%w==0} #rows, columns
- dirs[0].extend([w, w+1])
- dirs[w-1].append(w-1)
- def get_spot_coords(start, step): #generate row, column or diagonal coords
- end = w * step + start
- return [divmod(i,w) for i in range(start, end, step)]
- target = 0 #target sum
- for start in dirs.keys():
- for step in dirs[start]:
- temp = [grid[x][y] for x,y in get_spot_coords(start, step)]
- if temp.count(0)==0: target = sum(temp)
- while not all(grid[i//w][i%w] > 0 for i in range(w*w)):
- for start in dirs.keys():
- for step in dirs[start]:
- spot = get_spot_coords(start, step)
- if sum(grid[x][y]>0 for x, y in spot) == w-1:
- x, y = [(x,y) for x,y in spot if grid[x][y]==0][0]
- grid[x][y] = target - sum(grid[i][j] for i,j in spot)
- print(*grid, sep='\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement