Spaces:
Running
Running
| file = "input.txt" | |
| def parse_input(file): | |
| robots = [] | |
| with open(file, 'r') as f: | |
| for line in f: | |
| parts = line.strip().split() | |
| pos = tuple(map(int, parts[0][2:].split(','))) | |
| vel = tuple(map(int, parts[1][2:].split(','))) | |
| robots.append((pos, vel)) | |
| return robots | |
| def simulate_robots(robots, width, height, seconds): | |
| positions = [] | |
| for pos, vel in robots: | |
| x, y = pos | |
| vx, vy = vel | |
| # Calculate new position after 'seconds' seconds | |
| new_x = (x + vx * seconds) % width | |
| new_y = (y + vy * seconds) % height | |
| positions.append((new_x, new_y)) | |
| return positions | |
| def count_quadrants(positions, width, height): | |
| mid_x = width // 2 | |
| mid_y = height // 2 | |
| quadrants = [0, 0, 0, 0] # Q1, Q2, Q3, Q4 | |
| for x, y in positions: | |
| if x == mid_x or y == mid_y: | |
| continue # Skip robots exactly on the middle lines | |
| if x < mid_x and y < mid_y: | |
| quadrants[0] += 1 # Q1 | |
| elif x >= mid_x and y < mid_y: | |
| quadrants[1] += 1 # Q2 | |
| elif x < mid_x and y >= mid_y: | |
| quadrants[2] += 1 # Q3 | |
| elif x >= mid_x and y >= mid_y: | |
| quadrants[3] += 1 # Q4 | |
| return quadrants | |
| def calculate_safety_factor(quadrants): | |
| return quadrants[0] * quadrants[1] * quadrants[2] * quadrants[3] | |
| robots = parse_input(file) | |
| width, height = 101, 103 | |
| seconds = 100 | |
| positions_after_100_seconds = simulate_robots(robots, width, height, seconds) | |
| quadrants = count_quadrants(positions_after_100_seconds, width, height) | |
| safety_factor = calculate_safety_factor(quadrants) | |
| print(safety_factor) |