mirror of
https://github.com/thib8956/advent-of-code.git
synced 2025-01-14 22:01:05 +00:00
[run.py] support year parameter
This commit is contained in:
parent
5e599b0f3a
commit
89b6b5e107
15
README.md
15
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 <year> <day>
|
||||
```
|
||||
|
||||
### Run a whole year
|
||||
|
||||
```shell
|
||||
$ python3 run.py <year>
|
||||
```
|
||||
|
||||
|
@ -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__} <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)
|
||||
|
Loading…
Reference in New Issue
Block a user