-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
executable file
·55 lines (47 loc) · 1.38 KB
/
helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
"""
AOC Runner
"""
import argparse
import re
from pathlib import Path
from subprocess import Popen, PIPE
parser = argparse.ArgumentParser(description="Advent of Code helper")
parser.add_argument("program", type=str, help="The program to run")
parser.add_argument(
"--attempt",
dest="source",
action="store_const",
const="input",
default="example",
help="Attempt to run with the solution",
)
args = parser.parse_args()
program = Path(args.program)
if match := re.search(r"\d{2}", program.name):
day = match.group(0)
else:
raise Exception(f"Program name {program.name!r} does not conatin a 2 digit number.")
input_file = str(program.parent / "inputs" / f"{args.source}_{day}.txt")
process = Popen(
[f"{program.parent}/{program.name}", input_file], stdout=PIPE, stderr=PIPE
)
(output, err) = process.communicate()
exit_code = process.wait()
output, err = output.decode("utf-8"), err.decode("utf-8")
if exit_code != 0:
if output:
print(output)
if err:
print(err)
raise Exception("Program exited with non-zero exitcode.")
with open(
program.parent / "outputs" / f"{args.source}_{day}.txt", encoding="utf-8"
) as f:
expected_output = f.read()
if output != expected_output:
print("Output does not match expected input.")
print("\nOutput:")
print(output)
print("Expected:")
print(expected_output)