import math import re # SECONDS = 7519 SECONDS = 10000 with open('input.txt', 'r') as file: data: list[str] = file.readlines() class Coord: x: int y: int def __init__(self, x: int, y: int): self.x = x self.y = y def __str__(self) -> str: return f'({self.x}, {self.y})' def __eq__(self, other) -> bool: return other.x == self.x and other.y == self.y def __add__(self, other): return Coord(self.x + other.x, self.y + other.y) def __sub__(self, other): return Coord(self.x - other.x, self.y - other.y) def __mul__(self, other): return Coord(self.x * other, self.y * other) def __rmul__(self, other): return Coord(self.x * other, self.y * other) class Robot: pos: Coord vel: Coord def __init__(self, pos: Coord, vel: Coord): self.pos = pos self.vel = vel def __str__(self) -> str: return f'p={self.pos.x},{self.pos.y} v={self.vel.x},{self.vel.y}' robots: list[Robot] = [] WIDTH, HEIGHT = [int(n) for n in data[0].split(', ')] for line in data[1:]: p1, p2, v1, v2 = [int(n) for n in re.findall('-?[0-9]+', line)] robots.append(Robot(Coord(p1, p2), Coord(v1, v2))) def time_passes(robots: list[Robot], seconds: int) -> list[Robot]: for robot in robots: robot.pos = robot.pos + (robot.vel * seconds) while robot.pos.x >= WIDTH: robot.pos.x = robot.pos.x - WIDTH while robot.pos.x < 0: robot.pos.x = WIDTH + robot.pos.x while robot.pos.y >= HEIGHT: robot.pos.y = robot.pos.y - HEIGHT while robot.pos.y < 0: robot.pos.y = HEIGHT + robot.pos.y return robots def print_robo_map(robots: list[Robot]) -> None: robomap = [[0] * WIDTH for i in range(HEIGHT)] for robot in robots: robomap[robot.pos.y][robot.pos.x] += 1 for row in robomap: for c in row: print('.' if c == 0 else c, end='') print() # Order is quad 1, 2, 3, 4 def count_quads(robots: list[Robot]) -> list[int, int, int, int]: ignore_y = HEIGHT // 2 ignore_x = WIDTH // 2 quads = [0, 0, 0, 0] for robot in robots: if robot.pos.x == ignore_x or robot.pos.y == ignore_y: continue # Quad 1 if robot.pos.x < ignore_x and robot.pos.y < ignore_y: quads[0] += 1 # Quad 2 elif robot.pos.x > ignore_x and robot.pos.y < ignore_y: quads[1] += 1 # Quad 3 elif robot.pos.x < ignore_x and robot.pos.y > ignore_y: quads[2] += 1 # Quad 4 elif robot.pos.x > ignore_x and robot.pos.y > ignore_y: quads[3] += 1 return quads robots = time_passes(robots, 46) for i in range(46, SECONDS, 101): robots = time_passes(robots, 101) print_robo_map(robots) print('Frame: ', i) input() print("\033[H\033[J", end="") # print_robo_map(robots) # print('Safety Factor: ', math.prod(count_quads(robots))) # smallest_sf = float('inf') # frame = 0 # for i in range(100000): # robots = time_passes(robots) # sf = math.prod(count_quads(robots)) # if sf < smallest_sf: # frame = i # smallest_sf = sf # print(frame)