This commit is contained in:
2024-12-31 23:25:28 +01:00
parent 48c02c06ea
commit 7ab66a8e6f
5 changed files with 2107 additions and 0 deletions

46
07/07-1.py Normal file
View 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')

53
07/07-2.py Normal file
View File

@@ -0,0 +1,53 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
file = "./input.txt"
#file = "./ex2.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:
triple_in = []
triple_out = []
inside_brackets = False
for i in range(len(l)-2):
if l[i+1] in ["[", "]"]:
continue
elif 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+2]:
triple_in.append(f"{l[i]}{l[i+1]}{l[i+2]}")
elif not inside_brackets and l[i] != l[i+1] and l[i] == l[i+2]:
triple_out.append(f"{l[i]}{l[i+1]}{l[i+2]}")
for t in triple_in:
if f"{t[1]}{t[0]}{t[1]}" in triple_out:
return True
return False
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')

4
07/ex.txt Normal file
View File

@@ -0,0 +1,4 @@
abba[mnop]qrst
abcd[bddb]xyyx
aaaa[qwer]tyui
ioxxoj[asdfgh]zxcvbn

4
07/ex2.txt Normal file
View File

@@ -0,0 +1,4 @@
aba[bab]xyz
xyx[xyx]xyx
aaa[kek]eke
zazbz[bzb]cdb

2000
07/input.txt Normal file

File diff suppressed because it is too large Load Diff