-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_5.py
101 lines (74 loc) · 3.24 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
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
from laurens.util.file_reader import read_from_file
TEST = True
def get_conversion_lists(values: list):
conversion_lists = []
conversion_list = []
for i in range(2, len(values)):
if values[i].strip() == "":
conversion_lists.append(conversion_list.copy())
conversion_list = []
continue
if values[i][0].isdigit():
conversion_list.append(values[i].split(" "))
conversion_lists.append(conversion_list.copy())
return conversion_lists
def task_1(values: list):
seeds = values[0].split(":")[1].strip().split(" ")
conversion_lists = get_conversion_lists(values)
location_entries = conversion_lists.pop()
conversion_lists_reversed = reversed(conversion_lists)
location = 0
found_location = -1
while found_location == -1:
for index, entry in enumerate(location_entries):
previous_index = -1
if location in range(int(entry[0]), int(entry[0]) + int(entry[2])):
previous_index = location - int(entry[0]) + int(entry[1])
if index == len(location_entries) - 1:
previous_index = location
if previous_index != -1:
for conversion_list in conversion_lists_reversed:
for e in conversion_list:
if previous_index in range(int(e[0]), int(e[0]) + int(e[2]) + 1):
previous_index = previous_index - int(e[0]) + int(e[1])
break
if str(previous_index) in seeds:
return location
location += 1
def task_2(values: list):
found_values = values[0].split(":")[1].strip().split(" ")
seed_ranges = []
while len(found_values) > 0:
first_index = int(found_values.pop(0))
found_range = int(found_values.pop(0))
seed_ranges.append([first_index, first_index + found_range])
conversion_lists = get_conversion_lists(values)
location_entries = conversion_lists.pop()
conversion_lists_reversed = list(reversed(conversion_lists))
location = 0
found_location = -1
while found_location == -1:
for index, entry in enumerate(location_entries):
previous_index = -1
if location in range(int(entry[0]), int(entry[0]) + int(entry[2])):
previous_index = location - int(entry[0]) + int(entry[1])
if index == len(location_entries) - 1:
previous_index = location
if previous_index != -1:
for conversion_list in conversion_lists_reversed:
for e in conversion_list:
if previous_index in range(int(e[0]), int(e[0]) + int(e[2]) + 1):
previous_index = previous_index - int(e[0]) + int(e[1])
break
for seed_range in seed_ranges:
if seed_range[0] <= previous_index <= seed_range[1]:
return location
location += 1
if __name__ == '__main__':
values = []
if TEST:
values = read_from_file("./data/day_5_test.txt")
else:
values = read_from_file("./data/day_5.txt")
print("Task 1: " + str((task_1(values))))
print("Task 2: " + str((task_2(values))))