Spaces:
Running
Running
File size: 1,666 Bytes
a4da721 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
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) |