import os import re from collections import Counter dir = os.path.dirname(os.path.abspath(__file__)) with open(os.path.join(dir, './input.txt'), 'r') as input_file: lines = input_file.readlines() max_x = 0 max_y = 0 parse_pattern = re.compile("(\d+),(\d+) -> (\d+),(\d+)") map = Counter() for line in lines: matches = re.search(parse_pattern, line.strip()) x1 = int(matches.group(1)) y1 = int(matches.group(2)) x2 = int(matches.group(3)) y2 = int(matches.group(4)) if x1 == x2: start = min(y1,y2) end = max(y1,y2) for y in range(start, end+1): key = f"{x1},{y}" map[key] = map[key] + 1 elif y1 == y2: start = min(x1,x2) end = max(x1,x2) for x in range(start, end+1): key = f"{x},{y1}" map[key] = map[key] + 1 count = 0 for keys in map: if map[keys] >= 2: count = count + 1 print(count)