You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.9 KiB
72 lines
1.9 KiB
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))
|
|
print(line.strip())
|
|
|
|
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
|
|
else:
|
|
if x1 < x2:
|
|
x_start = x1
|
|
x_end = x2
|
|
y_start = y1
|
|
y_end = y2
|
|
else:
|
|
x_start = x2
|
|
x_end = x1
|
|
y_start = y2
|
|
y_end = y1
|
|
|
|
x = x_start
|
|
y = y_start
|
|
|
|
if y_start < y_end:
|
|
while x != x_end+1 and y != y_end+1:
|
|
key = f"{x},{y}"
|
|
map[key] = map[key] + 1
|
|
x = x+1
|
|
y = y+1
|
|
else:
|
|
while x != x_end+1 and y != y_end-1:
|
|
key = f"{x},{y}"
|
|
print(key)
|
|
map[key] = map[key] + 1
|
|
x = x+1
|
|
y = y-1
|
|
|
|
|
|
count = 0
|
|
for keys in map:
|
|
if map[keys] >= 2:
|
|
count = count + 1
|
|
print(map)
|
|
print(count)
|