Files
AdventOfCode2016/07/07-2.py
2024-12-31 23:25:28 +01:00

53 lines
1.3 KiB
Python

#!/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')