#!/usr/bin/env python3 import sys import re class Bot: def __init__(self, x, y, dx, dy): self.x = x self.y = y self.dx = dx self.dy = dy def __str__(self): return f"Bot({self.x}, {self.y}, {self.dx}, {self.dy})" lre = re.compile("p=(\d+),(\d+) v=(-?\d+),(-?\d+)") def viewmap(bots, maxxy, maxy): locs = {} for b in bots: locs[(b.x, b.y)] = locs.get((b.x, b.y), 0) + 1 for y in range(maxy): for x in range(maxx): if (x, y) in locs: print(locs[x, y], end="") else: print(" ", end="") print("") def cycle(bots, maxx, maxy): for b in bots: b.x = (b.x + b.dx) % maxx b.y = (b.y + b.dy) % maxy def score(bots, maxx, maxy): quads = [0, 0, 0, 0] # top-left, top-right, bot-left, bot-right for b in bots: if b.x == maxx // 2 or b.y == maxy // 2: continue else: quad = 0 if b.x > maxx // 2: quad += 1 if b.y > maxy // 2: quad += 2 quads[quad] += 1 return quads[0] * quads[1] * quads[2] * quads[3] def parse(): bots = [] maxx = 0 maxy = 0 for l in sys.stdin: m = lre.match(l) nums = [int(x) for x in m.groups()] b = Bot(*nums) bots.append(b) if b.x > maxx: maxx = b.x if b.y > maxy: maxy = b.y return bots, maxx + 1, maxy + 1 if __name__ == '__main__': bots, maxx, maxy = parse() for i in range(10000): if i % maxx == 39 or i % maxy == 99: print(f"{i} ---------------") viewmap(bots, maxx, maxy) cycle(bots, maxx, maxy) print(score(bots, maxx, maxy))