#!/usr/bin/env python3 import sys import re def parse(line): groups = re.compile(r"(?P\d+),(?P\d+) -> (?P\d+),(?P\d+)").search(line) return groups def calc(x1, y1, x2, y2, mem): if x1 == x2: if y1 > y2: y = y2 y2 = y1 y1 = y for y in range(y1, y2+1): if (x1, y) not in mem: mem[(x1, y)] = 1 else: mem[(x1, y)] += 1 elif y1 == y2: if x1 > x2: x = x2 x2 = x1 x1 = x for x in range(x1, x2+1): if (x, y1) not in mem: mem[(x, y1)] = 1 else: mem[(x, y1)] += 1 def calc2(x1, y1, x2, y2): if x1 != x2 and y1 != y2: if x1 > x2: dif = x1-x2+1 m1 = -1 else: dif = x2-x1+1 m1 = 1 if y1 > y2: m2 = -1 else: m2 = 1 for i in range(dif): if (x1+m1*i, y1+m2*i) not in memo2: memo2[(x1+m1*i, y1+m2*i)] = 1 else: memo2[(x1+m1*i, y1+m2*i)] += 1 if __name__ == '__main__': lines = open(sys.argv[1]).readlines() coords = [tuple(map(int,parse(line).groups())) for line in lines] # challenge 1 memo = {} for c in coords: calc(*c,memo) res1 = 0 for v in memo.values(): if v > 1: res1 += 1 print("challenge 1:" + "\n" + str(res1) + "\n") # challenge 2 memo2 = {} for c in coords: calc(*c,memo2) calc2(*c) res2 = 0 for v in memo2.values(): if v > 1: res2 += 1 print("challenge 2:" + "\n" + str(res2) + "\n")