-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_5.py
68 lines (56 loc) · 2.47 KB
/
day_5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import re
from utils import read_file
def day_5():
input_data = [x for x in read_file(5, 'single')]
def line_from_endpoints(endpoints_string):
endpoints = [int(x) for x in re.findall(r'\d+', endpoints_string)]
if endpoints[2] > endpoints[0]:
x_direction = 1
elif endpoints[2] == endpoints[0]:
x_direction = 0
else:
x_direction = -1
if endpoints[3] > endpoints[1]:
y_direction = 1
elif endpoints[3] == endpoints[1]:
y_direction = 0
else:
y_direction = -1
# Horizontal
if y_direction == 0:
orthogonal = True
points = [(x, endpoints[1]) for x in range(endpoints[0], endpoints[2] + x_direction, x_direction)]
# Vertical
elif x_direction == 0:
orthogonal = True
points = [(endpoints[0], x) for x in range(endpoints[1], endpoints[3] + y_direction, y_direction)]
# Diagonal
else:
orthogonal = False
points = [(x, y) for x, y in zip(range(endpoints[0], endpoints[2] + x_direction, x_direction),
range(endpoints[1], endpoints[3] + y_direction, y_direction))]
if x_direction != 0 and y_direction != 0:
return orthogonal, points
else:
return orthogonal, points
def generate_point_visits(data):
points_visited = set()
points_visited_more_than_once = set()
points_visited_with_diagonals = set()
points_visited_more_than_once_with_diagonals = set()
for line in data:
orthogonal, generated_points = line_from_endpoints(line)
for point in generated_points:
# Orthogonal
if orthogonal and point not in points_visited:
points_visited.add(point)
elif orthogonal and point not in points_visited_more_than_once:
points_visited_more_than_once.add(point)
# Diagonal
if point not in points_visited_with_diagonals:
points_visited_with_diagonals.add(point)
elif point not in points_visited_more_than_once_with_diagonals:
points_visited_more_than_once_with_diagonals.add(point)
return len(points_visited_more_than_once), len(points_visited_more_than_once_with_diagonals)
results = generate_point_visits(input_data)
return results[0], results[1]