advent-of-code/2019-python/aoc.py

122 lines
2.7 KiB
Python
Raw Normal View History

2023-12-19 18:34:03 +01:00
import os
2023-11-21 09:42:24 +01:00
import sys
2023-12-19 18:34:03 +01:00
def headline(n, title):
"""Print day number and name, followed by a ruler. Used by the answer decorator"""
print(f"\n--- Day {n}: {title} ---\n")
2023-11-21 09:42:24 +01:00
year = 2019
try:
2023-11-27 16:33:09 +01:00
_, day_no, *name = sys.argv
2023-11-21 09:42:24 +01:00
except ValueError:
day_no = None
name = None
print(
f"\nAdvent of Code {year}" "\n###################" "\n\nby Anders Englöf Ytterström"
)
if day_no and name:
2023-11-27 16:33:09 +01:00
name = " ".join(name)
2023-11-21 09:42:24 +01:00
padded_no = day_no.zfill(2)
print(f"\n- creating output/day_{padded_no}.py")
with open("output/day_{}.py".format(padded_no), "w") as s:
s.write(
f"""
2023-12-19 18:34:03 +01:00
from output import answer # , matrix, D, DD, ADJ, ints, mhd, mdbg, vdbg
2023-11-21 09:42:24 +01:00
n = {day_no}
title = "{name}"
@answer(1, "Answer is {{}}")
2023-12-19 18:34:03 +01:00
def part_1(outputs):
return outputs[0]
2023-11-21 09:42:24 +01:00
@answer(2, "Actually, answer is {{}}")
2023-12-19 18:34:03 +01:00
def part_2(outputs):
return outputs[1]
def solve(data):
return None, None
2023-11-21 09:42:24 +01:00
if __name__ == "__main__":
# use dummy data
2023-12-19 18:34:03 +01:00
inp = \"\"\"
2023-11-21 09:42:24 +01:00
replace me
\"\"\".strip()
# uncomment to instead use stdin
2023-12-19 18:34:03 +01:00
# import sys; inp = sys.stdin.read().strip()
# uncomment to use AoC provided puzzle input
# with open("./input/{padded_no}.txt", "r") as f:
# inp = f.read().strip()
2023-11-21 09:42:24 +01:00
2023-12-19 18:34:03 +01:00
inp = solve(inp)
2023-11-21 09:42:24 +01:00
2023-12-19 18:34:03 +01:00
a = part_1(inp)
# b = part_2(inp)
# uncomment and replace 0 with actual output to refactor code
# and ensure nonbreaking changes
# assert a == 0
# assert b == 0
2023-11-21 09:42:24 +01:00
""".strip()
+ "\n"
)
2023-12-19 18:34:03 +01:00
print("- making sure input dir exists")
if not os.path.exists("input"):
os.makedirs("input")
2023-11-21 09:42:24 +01:00
print(
f"""
Done! start coding.
Puzzle link:
https://adventofcode.com/{year}/day/{day_no}
Puzzle input (copy and paste to input/{day_no.zfill(2)}.txt):
https://adventofcode.com/{year}/day/{day_no}/input
"""
)
exit(0)
stars = 0
2023-11-21 09:42:24 +01:00
for i in [str(n).zfill(2) for n in range(1, 26)]:
2023-12-19 18:34:03 +01:00
if not day_no or day_no.zfill(2) == i:
try:
day = __import__(
"output.day_{}".format(i),
globals(),
locals(),
["n", "title", "part_1", "part_2", "solve"],
0,
)
with open(f"./input/{i}.txt", "r") as f:
data = f.read().strip()
headline(day.n, day.title)
try:
data = day.solve(data)
except AttributeError:
pass
if day.part_1(data, decorate=True):
stars += 1
if day.part_2(data, decorate=True):
stars += 1
except IOError:
pass
except ImportError:
pass
if not day_no:
print(f"\nStars: {stars}")
print("".join("*" if n < stars else "" for n in range(50)))
print("")