2024 day 15 part 1

This commit is contained in:
Thibaud Gasser 2024-12-17 16:58:25 +01:00
parent 8c449156f5
commit 71dc5fcd9d

View File

@ -48,8 +48,8 @@ def push(grid, pos, movement):
elif movement == "<": elif movement == "<":
start_x, end_x = int(start_pos.real), int(end_pos.real) start_x, end_x = int(start_pos.real), int(end_pos.real)
y = int(start_pos.imag) y = int(start_pos.imag)
for i in range(start_x, end_x + 1): for i in range(start_x, end_x - 1, -1):
grid[y][i+1] = "O" # shift "O" to the left grid[y][i-1] = "O" # shift "O" to the left
grid[y][start_x] = "." grid[y][start_x] = "."
elif movement == "v": elif movement == "v":
start_y, end_y = int(start_pos.imag), int(end_pos.imag) start_y, end_y = int(start_pos.imag), int(end_pos.imag)
@ -60,11 +60,20 @@ def push(grid, pos, movement):
elif movement == "^": elif movement == "^":
start_y, end_y = int(start_pos.imag), int(end_pos.imag) start_y, end_y = int(start_pos.imag), int(end_pos.imag)
x = int(start_pos.real) x = int(start_pos.real)
for i in range(end_y, start_y - 1, -1): for i in range(start_y, end_y - 1, -1):
grid[i+1][x] = "O" # shift "O" up grid[i-1][x] = "O" # shift "O" up
grid[start_y][x] = "." grid[start_y][x] = "."
def calculate_gps_coords(grid):
total = 0
for y, row in enumerate(grid):
for x, _ in enumerate(row):
if grid[y][x] == "O":
total += 100*y + x
return total
def main(content): def main(content):
grid, movements = content.split("\n\n") grid, movements = content.split("\n\n")
grid = [list(x) for x in grid.split("\n")] grid = [list(x) for x in grid.split("\n")]
@ -87,7 +96,9 @@ def main(content):
pos = new_pos pos = new_pos
case "#": pass case "#": pass
case c: raise RuntimeError("This should never happen", c) case c: raise RuntimeError("This should never happen", c)
debug_print(grid, movement) #debug_print(grid, movement)
part1 = calculate_gps_coords(grid)
print("Part 1: ", part1)
if __name__ == "__main__": if __name__ == "__main__":