Untitled
unknown
plain_text
3 years ago
4.0 kB
10
Indexable
# directions = {
# '>': 1,
# '<': -1
# }
def parse_directions():
return open("puzzle_input.txt").read()
def resize_grid(grid: list[str], rock: list[str]):
# highest = 0
# for line in grid:
# r = line.find('#')
# if r != -1:
# highest += 1
# else:
# break
rock_height = len(rock)
space_between = [['.'] * width] * 3
grid.insert(rock_height-1,space_between )
def insert_rock(rock, grid, width):
for i, line in enumerate(rock):
grid.insert(i, ['.'] * 2 + list(line) + ["."] * (width - len(line) - 2))
# resize_grid(grid, rock)
rock_height = len(rock)
space_between = ['.'] * width
for _ in range(3):
grid.insert(rock_height,space_between)
def next_idx(lst:list, curr_idx:int):
return curr_idx + 1 if curr_idx < len(lst) - 1 else 0
def print_grid(grid):
print("-------")
for line in grid:
print("".join(line))
def shift_block(grid:list[str], dir: tuple[int,int]):
dx, dy = dir
positions = []
for y in range(len(grid)):
for x in range(len(grid[0])):
if grid[y][x] == '@':
positions.append((x, y))
new_positions = [(x, y+1) for x, y in positions]
for x, y in positions: # remove block
grid[y][x] = '.'
for x, y in new_positions: # build shifted block
grid[y][x] = '@'
print_grid(grid)
print(new_positions)
return grid
# shifted = [row[:] for row in grid] # Create a new list and copy the values from grid into it
# rows = len(grid)
# cols = len(grid[0])
# dx, dy = dir
# for i in range(rows):
# for j in range(cols):
# char = shifted[i][j]
# if char == '@':
# if (i + dy < 0 or j + dx < 0 or i + dy >= rows or j + dx >= cols): # hit border
# print("hit border!")
# return grid
# if grid[i+dy][j+dx] == '#':
# print("hit other rock!")
# return grid
# # Shift the block of '@' characters
# shifted[i+dy][j+dx] = '@'
# if grid[i+dy][j+dx] == '.':
# break
# else:
# shifted[i][j] = '.'
# print(shifted)
# return shifted
# match dir:
# case 1:
# shifted = [row[-1] + row[:-1] for row in grid]
# case -1:
# shifted = [row[1:] + row[0] for row in grid]
# case _:
# shifted = [grid[(i - 1) % len(grid)] for i in range(len(grid))]
# return shifted
def simulate(rock_types, dirs, max_rocks, width):
grid = []
rock_count = 0
dir_idx = 0
rock_idx = 0
while rock_count < max_rocks: # while
rock = rock_types[rock_idx]
rock_idx = next_idx(rock_types, rock_idx)
rock_count += 1
insert_rock(rock, grid, width)
print_grid(grid)
grid = shift_block(grid, (0,1))
print_grid(grid)
return []
rest = False
while not rest: # while rock hasnt stopped moving
dir = 1 if dirs[dir_idx] == '>' else -1
dir_idx = next_idx(dirs, dir_idx)
# column_idx = 0 if dir == -1 else -1
# lateral_col = [grid[row][column_idx] for row in range(len(grid))]
# can_move_side = all(x == '.' for x in lateral_col)
grid = shift_block(grid, 1,0)
print_grid(grid)
return
# if rest, turn @ to # and break
# else continue
return rock_count
Editor is loading...