adventofcode-2021/05/solve02.py
2021-12-05 07:50:17 +01:00

41 lines
996 B
Python

#!/usr/bin/env python
import re
from collections import defaultdict
RE_INPUT = re.compile(r'^(\d+),(\d+)\s*\->\s*(\d+),(\d+)')
def read_file(filename):
file = open(filename, 'r')
while True:
line = file.readline()
if not line:
break
yield line
def process_vent(coord, see_map):
diff_x = coord[2] - coord[0]
diff_y = coord[3] - coord[1]
steps = max(abs(diff_x), abs(diff_y))
diff_x = diff_x/steps;
diff_y = diff_y/steps;
for i in range(steps+1):
k = "%d,%d" % (coord[0]+diff_x*i,coord[1]+diff_y*i)
see_map[k] += 1
def main():
see_map = defaultdict(int)
for line in read_file('input01.txt'):
match = re.search(RE_INPUT, line)
if not match:
print("wrong syntax")
vent = [int(i) for i in match.groups()]
process_vent(vent, see_map)
result = sum([1 for i in see_map.values() if i > 1])
print(result)
if __name__ == "__main__":
main()