2024-11-28 18:47:06 +01:00
|
|
|
import re,sys
|
|
|
|
|
|
|
|
def toGrid(input,parser=None):
|
|
|
|
grid = []
|
|
|
|
if parser:
|
|
|
|
with open(input) as file:
|
|
|
|
for line in file:
|
|
|
|
grid.append(list2int(line.rstrip()))
|
|
|
|
else:
|
|
|
|
with open(input) as file:
|
|
|
|
for line in file:
|
2024-11-29 19:02:00 +01:00
|
|
|
grid.append(list(line.rstrip()))
|
2024-11-28 18:47:06 +01:00
|
|
|
return grid
|
|
|
|
|
2024-12-05 18:10:12 +01:00
|
|
|
def swap(a:int,b:int,lst:list):
|
|
|
|
"""
|
|
|
|
Swaps two numbers in a list based on their indices.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
lst (list): The list of numbers.
|
|
|
|
index1 (int): The index of the first number.
|
|
|
|
index2 (int): The index of the second number.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list: The list after swapping the two numbers.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if a < 0 or b < 0 or a >= len(lst) or b >= len(lst):
|
|
|
|
raise IndexError("Index out of range.")
|
|
|
|
|
|
|
|
lst[a], lst[b] = lst[b], lst[a]
|
|
|
|
return lst
|
|
|
|
|
2024-12-04 20:53:21 +01:00
|
|
|
def addTuples(x:tuple,y:tuple):
|
|
|
|
return (x[0]+y[0],x[1]+y[1])
|
2024-12-01 12:58:38 +01:00
|
|
|
|
|
|
|
def findDupes(input:list):
|
|
|
|
# Returns the indicies of duplicate values in list
|
|
|
|
return [item for item in set(input) if input.count(item) > 1]
|
|
|
|
|
2024-11-30 20:13:48 +01:00
|
|
|
def lprint(x:str,log:bool):
|
|
|
|
if log:
|
|
|
|
print(x)
|
|
|
|
|
2024-11-29 19:02:00 +01:00
|
|
|
def expand_grid(grid):
|
|
|
|
num_rows = len(grid)
|
|
|
|
num_cols = len(grid[0])
|
|
|
|
|
|
|
|
expanded_grid = []
|
|
|
|
|
|
|
|
expanded_grid.append(['.'] * (num_cols + 2))
|
|
|
|
|
|
|
|
for row in grid:
|
|
|
|
expanded_grid.append(['.'] + row + ['.'])
|
|
|
|
|
|
|
|
expanded_grid.append(['.'] * (num_cols + 2))
|
|
|
|
|
|
|
|
return expanded_grid
|
|
|
|
|
|
|
|
def getCenter(grid):
|
|
|
|
return (int(len(grid)/2),int(len(grid[0])/2))
|
|
|
|
|
|
|
|
def nprint(grid,cur:set=None,sign:str=None):
|
2024-11-28 18:47:06 +01:00
|
|
|
for idx,i in enumerate(grid):
|
|
|
|
for jdx,j in enumerate(i):
|
2024-11-29 19:02:00 +01:00
|
|
|
if (idx,jdx) == cur:
|
|
|
|
if len(sign) > 1:
|
|
|
|
print(sign[0]+grid[idx][jdx]+sign[1],end='')
|
|
|
|
else:
|
|
|
|
print(sign,end=' ')
|
|
|
|
else:
|
|
|
|
print(grid[idx][jdx],end=' ')
|
2024-11-28 18:47:06 +01:00
|
|
|
print()
|
2024-11-28 15:10:22 +01:00
|
|
|
|
2024-11-24 19:14:52 +01:00
|
|
|
def list2int(x):
|
|
|
|
return list(map(int, x))
|
2024-11-25 12:27:11 +01:00
|
|
|
|
2024-12-04 20:53:21 +01:00
|
|
|
def grid_valid(x,y,grid):
|
|
|
|
rows = len(grid)
|
|
|
|
cols = len(grid[0])
|
|
|
|
return 0 <= x < rows and 0 <= y < cols
|
|
|
|
|
2024-11-28 15:10:22 +01:00
|
|
|
def get_re(pattern,str):
|
|
|
|
match = re.match(pattern, str)
|
|
|
|
if match:
|
|
|
|
return match
|
|
|
|
return None
|
|
|
|
|
2024-11-25 12:27:11 +01:00
|
|
|
def ppprint(x):
|
|
|
|
for idx,i in enumerate(x):
|
|
|
|
for jdx,j in enumerate(i):
|
|
|
|
print(x[idx][jdx],end='')
|
|
|
|
print()
|
2024-11-28 13:58:15 +01:00
|
|
|
|
2024-12-04 20:53:21 +01:00
|
|
|
def get_value_in_direction(grid, position, direction=None,length=1,type:str=None):
|
2024-11-28 13:58:15 +01:00
|
|
|
"""
|
2024-12-04 20:53:21 +01:00
|
|
|
Get the value(s) in a specified direction from a given position in a grid.
|
2024-11-28 13:58:15 +01:00
|
|
|
If no direction is provided, returns the value at the current position.
|
|
|
|
|
|
|
|
Parameters:
|
2024-12-04 20:53:21 +01:00
|
|
|
grid (list of list of int/float/str): The 2D grid.
|
2024-11-28 13:58:15 +01:00
|
|
|
position (set): A set containing x (row index) and y (column index) as integers.
|
|
|
|
direction (str, optional): The direction to check.
|
|
|
|
Options: 'up', 'down', 'left', 'right',
|
|
|
|
'up-left', 'up-right', 'down-left', 'down-right'.
|
2024-12-04 20:53:21 +01:00
|
|
|
length (int, optional): The number of steps to check in the given direction. Default is 1.
|
|
|
|
|
2024-11-28 13:58:15 +01:00
|
|
|
Returns:
|
2024-12-04 20:53:21 +01:00
|
|
|
list: A list of values in the grid for the specified direction and length.
|
|
|
|
Returns an empty list if any position is out of bounds.
|
2024-11-28 13:58:15 +01:00
|
|
|
"""
|
|
|
|
# Ensure the position is a set of two integers
|
|
|
|
if len(position) != 2 or not all(isinstance(coord, int) for coord in position):
|
|
|
|
raise ValueError("Position must be a set containing two integers (x, y).")
|
|
|
|
|
|
|
|
x, y = position
|
|
|
|
offsets = {
|
|
|
|
'up': (-1, 0),
|
|
|
|
'down': (1, 0),
|
|
|
|
'left': (0, -1),
|
|
|
|
'right': (0, 1),
|
|
|
|
'up-left': (-1, -1),
|
|
|
|
'up-right': (-1, 1),
|
|
|
|
'down-left': (1, -1),
|
|
|
|
'down-right': (1, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
# If no direction is given, return the value at the current position
|
|
|
|
if direction is None:
|
|
|
|
if 0 <= x < len(grid) and 0 <= y < len(grid[x]):
|
|
|
|
return grid[x][y]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Validate direction
|
|
|
|
if direction not in offsets:
|
|
|
|
raise ValueError(f"Invalid direction: {direction}. Choose from {list(offsets.keys())}")
|
|
|
|
|
|
|
|
dx, dy = offsets[direction]
|
|
|
|
new_x, new_y = x + dx, y + dy
|
|
|
|
|
2024-12-04 20:53:21 +01:00
|
|
|
values = []
|
|
|
|
|
|
|
|
if length == 1:
|
|
|
|
# Check for out-of-bounds, considering varying row lengths
|
|
|
|
if 0 <= new_x < len(grid) and 0 <= new_y < len(grid[new_x]):
|
|
|
|
return grid[new_x][new_y]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
for step in range(length):
|
|
|
|
print(step)
|
|
|
|
new_x, new_y = x + step * dx, y + step * dy
|
|
|
|
# Check for out-of-bounds, considering varying row lengths
|
|
|
|
if 0 <= new_x < len(grid) and 0 <= new_y < len(grid[new_x]):
|
|
|
|
values.append(grid[new_x][new_y])
|
|
|
|
else:
|
|
|
|
return [] # Return empty list if any position is out of bounds
|
|
|
|
if type == 'list':
|
|
|
|
return values
|
|
|
|
elif type == 'str':
|
|
|
|
return ''.join(values)
|
2024-11-28 13:58:15 +01:00
|
|
|
else:
|
2024-12-04 20:53:21 +01:00
|
|
|
return values
|
|
|
|
|