From c9447851f531e105d512a7347d6e8fbbe5566b5b Mon Sep 17 00:00:00 2001 From: Andres Date: Fri, 6 Dec 2024 20:37:46 +0100 Subject: [PATCH] 2015 day 1 part 2 refactor --- 2015/day-1/part-1.py | 6 ++---- 2015/day-1/part-2.py | 13 +++++-------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/2015/day-1/part-1.py b/2015/day-1/part-1.py index 2b7bca6..da8ebd4 100644 --- a/2015/day-1/part-1.py +++ b/2015/day-1/part-1.py @@ -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) diff --git a/2015/day-1/part-2.py b/2015/day-1/part-2.py index 7ccf8a2..79eb4e9 100644 --- a/2015/day-1/part-2.py +++ b/2015/day-1/part-2.py @@ -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