48 lines
1.2 KiB
Python
48 lines
1.2 KiB
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):
|
||
|
if coord[0] == coord[2]:
|
||
|
c_max = max(coord[1], coord[3])+1
|
||
|
c_min = min(coord[1], coord[3])
|
||
|
|
||
|
for i in range(c_min, c_max):
|
||
|
k = "%d,%d" % (coord[0], i)
|
||
|
see_map[k] += 1
|
||
|
if coord[1] == coord[3]:
|
||
|
c_max = max(coord[0], coord[2])+1
|
||
|
c_min = min(coord[0], coord[2])
|
||
|
|
||
|
for i in range(c_min, c_max):
|
||
|
k = "%d,%d" % (i, coord[1])
|
||
|
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()]
|
||
|
if (not (vent[0] == vent[2] or vent[1] == vent[3])):
|
||
|
continue
|
||
|
process_vent(vent, see_map)
|
||
|
|
||
|
result = sum([1 for i in see_map.values() if i > 1])
|
||
|
print(result)
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|