[run.py] support year parameter

This commit is contained in:
Thibaud Gasser 2025-01-04 15:20:08 +01:00
parent 5e599b0f3a
commit 89b6b5e107
2 changed files with 32 additions and 10 deletions

View File

@ -1,3 +1,18 @@
# Advent of code # Advent of code
My solutions to the [advent of code](https://adventofcode.com/) challenges, written in python 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 <year> <day>
```
### Run a whole year
```shell
$ python3 run.py <year>
```

View File

@ -4,9 +4,9 @@ import subprocess
from pathlib import Path from pathlib import Path
def main(day): def main(year, day):
if day is not None: if day is not None:
path = Path(f"day{day}") path = Path(f"{year}/day{day}")
script_path = path / Path(f"day{day}.py") script_path = path / Path(f"day{day}.py")
input_path = path / Path("input.txt") input_path = path / Path("input.txt")
if not script_path.exists(): if not script_path.exists():
@ -18,7 +18,7 @@ def main(day):
run_day(script_path, input_path) run_day(script_path, input_path)
else: else:
for day in range(1, 26): 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") script_path = path / Path(f"day{day}.py")
input_path = path / Path("input.txt") input_path = path / Path("input.txt")
if script_path.exists(): if script_path.exists():
@ -29,14 +29,21 @@ def main(day):
def run_day(script_path, input_path): def run_day(script_path, input_path):
try:
start = time.time() start = time.time()
res = subprocess.run([sys.executable, script_path, input_path], check=True, stdout=subprocess.PIPE) res = subprocess.run([sys.executable, script_path.absolute(), input_path.absolute()], check=True, stdout=subprocess.PIPE, timeout=30)
elapsed = time.time() - start elapsed = time.time() - start
#print(res.stdout) #print(res.stdout)
print(f"ran {script_path} in {elapsed:.3f}s") 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__": if __name__ == "__main__":
day = sys.argv[1] if len(sys.argv) > 1 else None if len(sys.argv) <= 1:
main(day) print(f"Usage: {__file__} <year> [<day>]", file=sys.stderr)
exit(1)
year = sys.argv[1]
day = sys.argv[2] if len(sys.argv) > 2 else None
main(year, day)