fiddled with 2017.03.2 not working
This commit is contained in:
		
							parent
							
								
									6ddcc106ec
								
							
						
					
					
						commit
						e01bd4c86d
					
				
							
								
								
									
										167
									
								
								2017/03/2spiral_memory.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										167
									
								
								2017/03/2spiral_memory.py
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,167 @@ | ||||
| #!/home/dusk/.local/share/pipx/venvs/numpy/bin/python3 | ||||
| 
 | ||||
| import numpy | ||||
| if __name__ == '__main__': | ||||
|     target = 277678 | ||||
|     colstep = 1 | ||||
|     rowstep = -1 | ||||
|     square = 1 | ||||
|     pointer = [0, 0] | ||||
|     memory = [[1]] | ||||
|     value = 0 | ||||
| 
 | ||||
|     def grow_right(array): | ||||
|         new_array = [] | ||||
|         i = 0 | ||||
|         for row in array: | ||||
|             new_row = row + [0] | ||||
|             new_array = new_array + [new_row] | ||||
|             i += 1 | ||||
|         return new_array | ||||
| 
 | ||||
|     def grow_left(array): | ||||
|         new_array = [] | ||||
|         i = 0 | ||||
|         for row in array: | ||||
|             new_row = [0] + row | ||||
|             new_array = new_array + [new_row] | ||||
|             i += 1 | ||||
|         return new_array | ||||
| 
 | ||||
|     def grow_down(array): | ||||
|         row_width = len(array[0]) | ||||
|         new_array = array + numpy.zeros(row_width) | ||||
|         return new_array | ||||
| 
 | ||||
|     def grow_up(array): | ||||
|         row_width = len(array[0]) | ||||
|         new_array = numpy.zeros(row_width) + array | ||||
|         return new_array | ||||
| 
 | ||||
|     def get_adj(array, row, col, coords): | ||||
|         adj_value = 0 | ||||
|         for coord in coords: | ||||
|             pointer_row = row | ||||
|             pointer_col = col | ||||
|             match coord: | ||||
|                 case 0: | ||||
|                     pointer_col += 1 | ||||
|                 case 1: | ||||
|                     pointer_col += 1 | ||||
|                     pointer_row += -1 | ||||
|                 case 2: | ||||
|                     pointer_row += -1 | ||||
|                 case 3: | ||||
|                     pointer_row += -1 | ||||
|                     pointer_col += -1 | ||||
|                 case 4: | ||||
|                     pointer_col += -1 | ||||
|                 case 5: | ||||
|                     pointer_col += -1 | ||||
|                     pointer_row += 1 | ||||
|                 case 6: | ||||
|                     pointer_row += 1 | ||||
|                 case 7: | ||||
|                     pointer_row += 1 | ||||
|                     pointer_col += 1 | ||||
|             print(array) | ||||
|             adj_value += array[pointer_row][pointer_col] | ||||
|         adj_value = int(adj_value) | ||||
|         print(adj_value) | ||||
|         return adj_value | ||||
| 
 | ||||
|     def get_new_value(array, pointer): | ||||
|         adj_values = numpy.zeros(8) | ||||
|         # adjacent square coordinates: | ||||
|         # 3  2  1 | ||||
|         # 4  *  0 | ||||
|         # 5  6  7 | ||||
|         height = len(array) | ||||
|         width = len(array[0]) | ||||
|         row = pointer[0] | ||||
|         col = pointer[1] | ||||
|         is_right = False | ||||
|         is_up = False | ||||
|         is_left = False | ||||
|         is_down = False | ||||
|         if row == 0: | ||||
|             is_up = True | ||||
|         if row == (height - 1): | ||||
|             is_down = True | ||||
|         if col == 0: | ||||
|             is_left = True | ||||
|         if col == (width - 1): | ||||
|             is_right = True | ||||
| 
 | ||||
|         new_value = 0 | ||||
| 
 | ||||
|         if not is_right: | ||||
|             new_value += get_adj(array, row, col, [0]) | ||||
|         if not is_up: | ||||
|             new_value += get_adj(array, row, col, [2]) | ||||
|         if not is_left: | ||||
|             new_value += get_adj(array, row, col, [4]) | ||||
|         if not is_down: | ||||
|             new_value += get_adj(array, row, col, [6]) | ||||
| 
 | ||||
|         if (not is_right) and (not is_up): | ||||
|             new_value += get_adj(array, row, col, [1]) | ||||
|         if (not is_up) and (not is_left): | ||||
|             new_value += get_adj(array, row, col, [3]) | ||||
|         if (not is_left) and (not is_down): | ||||
|             new_value += get_adj(array, row, col, [5]) | ||||
|         if (not is_down) and (not is_right): | ||||
|             new_value += get_adj(array, row, col, [7]) | ||||
| 
 | ||||
|         if is_right and is_up and is_left and is_down: | ||||
|             new_value += 1 | ||||
| 
 | ||||
|         new_value = int(new_value) | ||||
|         return new_value | ||||
| 
 | ||||
|     def increment_step(step): | ||||
|         sign = int(step / abs(step)) # +/- 1 | ||||
|         new_sign = int(-1 * sign) | ||||
|         new_step = int(new_sign * (abs(step) + 1)) | ||||
|         return new_step | ||||
| 
 | ||||
|     while value <= target: | ||||
|         # colstep | ||||
|         i = 1 | ||||
|         if colstep >= 0: | ||||
|             memory = grow_right(memory) | ||||
|         else: | ||||
|             memory = grow_left(memory) | ||||
|             pointer[1] += 1 | ||||
|         while value <= target and i <= abs(colstep): | ||||
|             square += 1 | ||||
|             # step to next col | ||||
|             if colstep >= 0: | ||||
|                 pointer[1] += 1 | ||||
|             else: | ||||
|                 pointer[1] += -1 | ||||
|             value = get_new_value(memory, pointer) | ||||
|             memory[pointer[0]][pointer[1]] = value | ||||
|             i += 1 | ||||
|             print(memory) | ||||
|         colstep = increment_step(colstep) | ||||
|         # now for rowstep | ||||
|         i = 1 | ||||
|         if rowstep >= 0: | ||||
|             memory = grow_down(memory) | ||||
|         else: | ||||
|             memory = grow_up(memory) | ||||
|             pointer[0] += 1 | ||||
|         while value <= target and i <= abs(rowstep): | ||||
|             square += 1 | ||||
|             if rowstep >= 0: | ||||
|                 pointer[0] += 1 | ||||
|             else: | ||||
|                 pointer[0] += -1 | ||||
|             value = get_new_value(memory, pointer) | ||||
|             memory[pointer[0]][pointer[1]] = value | ||||
|             i += 1 | ||||
|             print(memory) | ||||
|         rowstep = increment_step(rowstep) | ||||
| 
 | ||||
|     print(value) | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user