-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
32 lines (23 loc) · 1.03 KB
/
main.py
File metadata and controls
32 lines (23 loc) · 1.03 KB
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
import time
def main(input_file, stage):
with open(input_file, "r") as file:
datastream = file.readline().strip("\n")
marker_start, marker, len_marker = 0, 0, (4 if stage == 1 else 14)
# first character should always be valid as there cannot be any duplicate yet
for i in range(1, len(datastream)):
# if our marker length is reached, we do not care about the next element anymore
if len(datastream[marker_start:i]) == len_marker:
marker = marker_start + len(datastream[marker_start:i])
break
if datastream[i] in datastream[marker_start:i]:
marker_start = datastream[:i].rindex(datastream[i])+1
print(marker)
if __name__ == "__main__":
use_example = False
file_name = "example" if use_example else "input"
start = time.time()
main(file_name, 1)
print(f"Stage 1 time: {time.time() - start}")
start = time.time()
main(file_name, 2)
print(f"Stage 2 time: {time.time() - start}")