07
This commit is contained in:
46
07/07-1.py
Normal file
46
07/07-1.py
Normal file
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
file = "./input.txt"
|
||||
#file = "./ex.txt"
|
||||
from time import time
|
||||
start_time = time()
|
||||
|
||||
def read_file(filename:str)->list[str]:
|
||||
result = []
|
||||
input_file = open(filename, "r")
|
||||
for line in input_file:
|
||||
line = line.strip()
|
||||
if line == "":
|
||||
continue
|
||||
else:
|
||||
result.append(line)
|
||||
input_file.close()
|
||||
return result
|
||||
|
||||
def check_line(l:str)->bool:
|
||||
result = False
|
||||
inside_brackets = False
|
||||
for i in range(len(l)-3):
|
||||
if l[i] == "[":
|
||||
inside_brackets = True
|
||||
continue
|
||||
elif l[i] == "]":
|
||||
inside_brackets = False
|
||||
continue
|
||||
elif inside_brackets and l[i] != l[i+1] and l[i] == l[i+3] and l[i+1] == l[i+2]:
|
||||
return False
|
||||
elif not inside_brackets and l[i] != l[i+1] and l[i] == l[i+3] and l[i+1] == l[i+2]:
|
||||
result = True
|
||||
return result
|
||||
|
||||
if __name__ == "__main__":
|
||||
data = read_file(file)
|
||||
sol = 0
|
||||
|
||||
for d in data:
|
||||
if check_line(d):
|
||||
sol += 1
|
||||
|
||||
print(f"Solution Part1: {sol}")
|
||||
print(f'Runtime: {time()-start_time:.4f} s')
|
||||
Reference in New Issue
Block a user