#!/usr/bin/python import re RE_LINE = re.compile('^#(\d+) @ (\d+),(\d+): (\d+)x(\d+)$') plan = dict() all = dict() overlap = dict() def read_input(): with open('input.txt', 'r') as f: for line in f: yield line.strip() def hash_position(x, y): return "{}x{}".format(x,y) for claim in read_input(): m = RE_LINE.match(claim) if not m: raise Exception("unparsable input: {}".format(claim)) for x in range(int(m.group(4))): for y in range(int(m.group(5))): hash = hash_position(x+int(m.group(2)), y+int(m.group(3))) claim_id = int(m.group(1)) all[claim_id] = True if hash not in plan: plan[hash] = [] plan[hash].append(claim_id) if len(plan[hash]) > 1: overlap[plan[hash][-1]] = True overlap[plan[hash][-2]] = True all = set(all.keys()) overlap = set(overlap.keys()) print(all - overlap)