-
Notifications
You must be signed in to change notification settings - Fork 1
/
day5.py
145 lines (131 loc) · 4.8 KB
/
day5.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import re
import timeit
from collections import defaultdict
def parse_data(file_name):
with open(file_name, "r") as f:
content = [line.strip() for line in f.readlines()]
list_of_tuples = [tuple(re.findall(r"\d+", line)) for line in content]
return list_of_tuples
# map for PART 1
def make_map_hv(input_list):
blank_map = [
[0 for i in range(1000)] for j in range(1000)
]
for line in input_list:
# columns
if line[0] == line[2]:
line_points_y = [int(line[1]), int(line[3])]
possition = int(line[0])
for i in range(min(line_points_y), max(line_points_y) + 1):
blank_map[i][possition] += 1
# rows
elif line[1] == line[3]:
line_points_x = [int(line[0]), int(line[2])]
line_number = int(line[1])
for j in range(min(line_points_x), max(line_points_x) + 1):
blank_map[line_number][j] += 1
return blank_map
# PART 2 (version A)
def part_2_with_map(file_name):
input_list = parse_data(file_name)
blank_map = [
[0 for i in range(1000)] for j in range(1000)
]
for line in input_list:
# columns
if line[0] == line[2]:
line_points_y = [int(line[1]), int(line[3])]
possition = int(line[0])
for i in range(min(line_points_y), max(line_points_y) + 1):
blank_map[i][possition] += 1
# rows
elif line[1] == line[3]:
line_points_x = [int(line[0]), int(line[2])]
line_number = int(line[1])
for j in range(min(line_points_x), max(line_points_x) + 1):
blank_map[line_number][j] += 1
# diagonals
else:
# X part
line_points_x = (int(line[0]), int(line[2]))
x_points = []
for i in range(min(line_points_x), max(line_points_x) + 1):
x_points.append(i)
if int(line[0]) > int(line[2]):
x_points.reverse()
# Y part
line_points_y = (int(line[1]), int(line[3]))
y_points = []
for i in range(min(line_points_y), max(line_points_y) + 1):
y_points.append(i)
if int(line[1]) > int(line[3]):
y_points.reverse()
# add points
for row, column in zip(y_points, x_points):
blank_map[row][column] += 1
return count_over_two(blank_map)
def count_over_two(map_of_vents):
more_than_one = 0
for i in map_of_vents:
for j in range(len(map_of_vents[0])):
if i[j] > 1:
more_than_one += 1
return more_than_one
# PART 2 (version B) - less work (no blank map)
def part_2_defaultdict(file_name):
input_list = parse_data(file_name)
vents = defaultdict(int)
for line in input_list:
# columns
if line[0] == line[2]:
line_points_y = [int(line[1]), int(line[3])]
possition = int(line[0])
for i in range(min(line_points_y), max(line_points_y) + 1):
vents[(i, possition)] += 1
# rows
elif line[1] == line[3]:
line_points_x = [int(line[0]), int(line[2])]
line_number = int(line[1])
for j in range(min(line_points_x), max(line_points_x) + 1):
vents[(line_number, j)] += 1
# diagonals
else:
# X part
line_points_x = (int(line[0]), int(line[2]))
x_points = []
for i in range(min(line_points_x), max(line_points_x) + 1):
x_points.append(i)
if int(line[0]) > int(line[2]):
x_points.reverse()
# Y part
line_points_y = (int(line[1]), int(line[3]))
y_points = []
for i in range(min(line_points_y), max(line_points_y) + 1):
y_points.append(i)
if int(line[1]) > int(line[3]):
y_points.reverse()
# add points
for row, column in zip(y_points, x_points):
vents[(row, column)] += 1
return len([value for value in vents.values() if value > 1])
def main(file_name):
input_list = parse_data(file_name)
map_of_vents = make_map_hv(input_list)
part_1 = count_over_two(map_of_vents)
part_2 = part_2_with_map(file_name)
return part_1, part_2
if __name__ == '__main__':
part1, part2 = main('day_5_in.txt')
print(f"Part one: {part1}")
print(f"Part two: {part2}")
print("Speed comparison")
print("Part two_A:", timeit.timeit(
"part_2_with_map('day_5_in.txt')",
setup="from __main__ import part_2_with_map",
number=1)
)
print("Part two_B:", timeit.timeit(
"part_2_defaultdict('day_5_in.txt')",
setup="from __main__ import part_2_defaultdict",
number=1)
)