from lib import get_data from lib import ints from collections import defaultdict rows = 103 cols = 101 data = get_data(__file__) def score(robots): quadrants = defaultdict(int) for _, r, c in robots: if r < rows // 2 and c < cols // 2: quadrants[1] += 1 elif r < rows // 2 and c > cols // 2: quadrants[2] += 1 elif r > rows // 2 and c < cols // 2: quadrants[3] += 1 elif r > rows // 2 and c > cols // 2: quadrants[4] += 1 t = 1 for q in quadrants.values(): t *= q return t def print_from_xy(xs): x_min = min(v[1] for v in xs) x_max = max(v[1] for v in xs) y_min = min(v[0] for v in xs) y_max = max(v[0] for v in xs) for y in range(y_min, y_max + 1): row = "" for x in range(x_min, x_max + 1): if (x, y) in xs: row += "#" else: row += " " print(row) robots_speed = {} robots = list() for i, line in enumerate(data.splitlines()): c, r, vc, vr = ints(line) robots_speed[i] = (vr, vc) robots.append((i, r, c)) for j in range(rows * cols): nrobots = list() count = 0 for i, r, c in robots: vr, vc = robots_speed[i] nr = (r + vr) % rows nc = (c + vc) % cols nrobots.append((i, nr, nc)) robots = nrobots if j == 99: print(score(robots)) robots_coords = [(r, c) for _, r, c in robots] if len(robots_coords) == len(set(robots_coords)): print(j + 1) # print_from_xy(robots_coords) break