diff --git a/README.md b/README.md index 95f0bd7..d558f3b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,18 @@ # Advent of code My solutions to the [advent of code](https://adventofcode.com/) challenges, written in python + +## How to run + +### Run a single day + +```shell +$ python3 run.py +``` + +### Run a whole year + +```shell +$ python3 run.py +``` + diff --git a/2024/run.py b/run.py similarity index 57% rename from 2024/run.py rename to run.py index 401247f..24ba9e0 100644 --- a/2024/run.py +++ b/run.py @@ -4,9 +4,9 @@ import subprocess from pathlib import Path -def main(day): +def main(year, day): if day is not None: - path = Path(f"day{day}") + path = Path(f"{year}/day{day}") script_path = path / Path(f"day{day}.py") input_path = path / Path("input.txt") if not script_path.exists(): @@ -18,7 +18,7 @@ def main(day): run_day(script_path, input_path) else: for day in range(1, 26): - path = Path(f"day{day}") + path = Path(f"{year}/day{day}") script_path = path / Path(f"day{day}.py") input_path = path / Path("input.txt") if script_path.exists(): @@ -29,14 +29,21 @@ def main(day): def run_day(script_path, input_path): - start = time.time() - res = subprocess.run([sys.executable, script_path, input_path], check=True, stdout=subprocess.PIPE) - elapsed = time.time() - start - #print(res.stdout) - print(f"ran {script_path} in {elapsed:.3f}s") + try: + start = time.time() + res = subprocess.run([sys.executable, script_path.absolute(), input_path.absolute()], check=True, stdout=subprocess.PIPE, timeout=30) + elapsed = time.time() - start + #print(res.stdout) + print(f"ran {script_path} in {elapsed:.3f}s") + except subprocess.TimeoutExpired: + print(f"timeout {script_path} after 30s", file=sys.stderr) if __name__ == "__main__": - day = sys.argv[1] if len(sys.argv) > 1 else None - main(day) + if len(sys.argv) <= 1: + print(f"Usage: {__file__} []", file=sys.stderr) + exit(1) + year = sys.argv[1] + day = sys.argv[2] if len(sys.argv) > 2 else None + main(year, day)