Skip to content

Commit

Permalink
Implement execution of last task, bump version 0.1.2
Browse files Browse the repository at this point in the history
Took 14 minutes
  • Loading branch information
Liborsaf committed Dec 2, 2022
1 parent f2318b8 commit 724c9d5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setuptools.setup(
name='aoc-library',
version='0.1.1',
version='0.1.2',
description='Advent of Code Library',
author='Liborsaf, Thr0nSK',
author_email='liborsaf@wetian.eu',
Expand Down
35 changes: 14 additions & 21 deletions src/aoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def __init__(self, year):


class AdventOfCodeTask(ABC):
def __init__(self, input: str):
self.input = input
def __init__(self, task_input: str):
self.task_input = task_input

@abstractmethod
def run(self):
Expand Down Expand Up @@ -50,8 +50,8 @@ def register_task(self, day: int, task: Type[AdventOfCodeTask]):
self.registered_tasks[day] = task

def execute(self, day: int):
input = self.load_input(day)
task = self.registered_tasks[day](input)
task_input = self.load_input(day)
task = self.registered_tasks[day](task_input)

task.run()

Expand All @@ -60,36 +60,32 @@ def execute_all(self):
self.execute(day)

def execute_last(self):
pass
last_day = list(self.registered_tasks.keys())[-1]

self.execute(last_day)

def load_input(self, day: int) -> str:
def load_input(self, day: int) -> Optional[str]:
if self.config.testing:
return "Dummy"

input = None
task_input = None
cache_file = None

if self.config.cache_input:
cache_file = path.join(self.config.cache_directory, f"{day}.tmp")

if path.exists(cache_file):
with open(cache_file, "r") as file:
input = file.read()

if self.config.debug:
print(f"input {day} loaded")
task_input = file.read()

if not input:
input = self.fetch_input(day)
if not task_input:
task_input = self.fetch_input(day)

if self.config.cache_input:
with open(cache_file, "w") as file:
file.write(input)
file.write(task_input)

if self.config.debug:
print(f"input {day} cached")

return input
return task_input

def fetch_input(self, day: int) -> Optional[str]:
response = requests.get(f"https://adventofcode.com/{self.config.year}/day/{day}/input", cookies={'session': self.config.session})
Expand All @@ -99,7 +95,4 @@ def fetch_input(self, day: int) -> Optional[str]:

return None

if self.config.debug:
print(f"input {day} fetched")

return response.text
12 changes: 10 additions & 2 deletions tests/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,20 @@ def test_aoc():
instance.register_task(4, LastTestTask)

instance.execute_last()
# assert last_task_executed, "Failed to execute last test task!"
assert last_task_executed, "Failed to execute last test task!"

global executed_tasks
assert executed_tasks == 3, "Wrong amount of tasks has been executed!" # Modify to 4 when execute_last will work
assert executed_tasks == 4, "Wrong amount of tasks has been executed!"

executed_tasks = 0

instance.execute_all()
assert executed_tasks == 4, "Wrong amount of tasks has been executed!"


def main():
test_aoc()


if __name__ == '__main__':
main()

0 comments on commit 724c9d5

Please sign in to comment.