mirror of
https://github.com/thib8956/advent-of-code.git
synced 2025-01-15 06:11:06 +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
|
# 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>
|
||||||
|
```
|
||||||
|
|
||||||
|
@ -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):
|
||||||
start = time.time()
|
try:
|
||||||
res = subprocess.run([sys.executable, script_path, input_path], check=True, stdout=subprocess.PIPE)
|
start = time.time()
|
||||||
elapsed = time.time() - start
|
res = subprocess.run([sys.executable, script_path.absolute(), input_path.absolute()], check=True, stdout=subprocess.PIPE, timeout=30)
|
||||||
#print(res.stdout)
|
elapsed = time.time() - start
|
||||||
print(f"ran {script_path} in {elapsed:.3f}s")
|
#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__":
|
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)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user