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: quadrant = 1 # Top-left quadrants[quadrant] += 1 elif r < rows // 2 and c > cols // 2: quadrant = 2 # Top-right quadrants[quadrant] += 1 elif r > rows // 2 and c < cols // 2: quadrant = 3 # Bottom-left quadrants[quadrant] += 1 elif r > rows // 2 and c > cols // 2: quadrant = 4 # Bottom-right quadrants[quadrant] += 1 else: pass t = 1 for q in quadrants.values(): t *= q return t def get_easter_egg_coords(rows, cols): coords = set() center_y, center_x = rows // 2, cols // 2 height = rows * 0.8 width = cols * 0.6 for i in range(rows): for j in range(cols): squeeze = 1 - 0.3 * ((i - center_y + height / 4) / height) egg_eq = (j - center_x) ** 2 / (width / 2) ** 2 + (i - center_y) ** 2 / ( (height / 2 * squeeze) ** 2 ) if egg_eq <= 1: coords.add((i, j)) return coords 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)) egg_coords = get_easter_egg_coords(rows, cols) max_count = 0 for j in range(10_000_000_000): nrobots = list() count = 0 for i, r, c in robots: vr, vc = robots_speed[i] nr = (r + vr) % rows nc = (c + vc) % cols if (nr, nc) in egg_coords: count += 1 nrobots.append((i, nr, nc)) robots = nrobots max_count = max(count, max_count) if j == 99: print(score(robots)) if count > 380: print(j + 1) # print_from_xy([(r, c) for _, r, c in robots]) break