2015 day 1 part 2 refactor

This commit is contained in:
2024-12-06 20:37:46 +01:00
parent 91dfd7bf54
commit c9447851f5
2 changed files with 7 additions and 12 deletions

View File

@@ -1,9 +1,7 @@
import os
abs_file_path = os.path.join(os.path.dirname(__file__), "./input.txt")
input = open(abs_file_path).read().strip()
file_path = os.path.join(os.path.dirname(__file__), "./input.txt")
input = open(file_path).read().strip()
result = input.count("(") - input.count(")")
print(result)

View File

@@ -1,17 +1,14 @@
import os
script_dir = os.path.dirname(__file__) # <-- absolute dir the script is in
rel_path = "./input.txt"
abs_file_path = os.path.join(script_dir, rel_path)
input = open(abs_file_path).read()
file_path = os.path.join(os.path.dirname(__file__), "./input.txt")
input = open(file_path).read().strip()
count = 0
for i, char in enumerate(list(input), start=1):
if count == 0 and char == ")":
print(i)
break
if char == "(":
count += 1
else:
if count == 0:
print(i)
break
count -= 1