Skip to content

Commit 9eb9918

Browse files
jameslawlorJames
andauthored
Restructure repo, add templates and new day file generation (#12)
* refactor and make daygen script * refactor --------- Co-authored-by: James <[email protected]>
1 parent 16a0d82 commit 9eb9918

26 files changed

+135
-29
lines changed

.github/workflows/orchestrator.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: aoc-2023-ci
1+
name: advent-of-code-python-ci
22

33
on: [push, pull_request]
44

Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ test:
2222

2323
# Run specific solutions for Advent of Code 2023
2424
solutions:
25-
python3 src/aoc_2023/days/1.py --input_file inputs/1.txt --part 1
26-
python3 src/aoc_2023/days/1.py --input_file inputs/1.txt --part 2
27-
python3 src/aoc_2023/days/2.py --input_file inputs/2.txt
28-
python3 src/aoc_2023/days/3.py --input_file inputs/3.txt
29-
python3 src/aoc_2023/days/4.py --input_file inputs/4.txt
30-
python3 src/aoc_2023/days/6.py --input_file inputs/6.txt
25+
python3 src/advent_of_code/year_2023/days/1.py --input_file inputs/2023/1.txt --part 1
26+
python3 src/advent_of_code/year_2023/days/1.py --input_file inputs/2023/1.txt --part 2
27+
python3 src/advent_of_code/year_2023/days/2.py --input_file inputs/2023/2.txt
28+
python3 src/advent_of_code/year_2023/days/3.py --input_file inputs/2023/3.txt
29+
python3 src/advent_of_code/year_2023/days/4.py --input_file inputs/2023/4.txt
30+
python3 src/advent_of_code/year_2023/days/6.py --input_file inputs/2023/6.txt

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# aoc-2023
1+
# advent-of-code-python
22

33
Some overengineered Python solutions for Advent of Code 2023
44

55
```
6-
docker build -t aoc-2023-app .
7-
docker run aoc-2023-app
6+
docker build -t advent-of-code-python-app .
7+
docker run advent-of-code-python-app
88
```
99

10-
### Progress
10+
### 2023 Progress
1111

1212
| Day | Part 1 | Part 2 |
1313
| :---: | :------: | :------: |

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[project]
2-
name = "aoc-2023"
2+
name = "advent-of-code-python"
33
version = "0.0.1"
44
authors = [
55
{ name="James Lawlor", email="[email protected]" },
@@ -14,7 +14,7 @@ classifiers = [
1414
]
1515

1616
[project.urls]
17-
Homepage = "https://github.com/jameslawlor/aoc-2023"
17+
Homepage = "https://github.com/jameslawlor/advent-of-code-python"
1818

1919
[build-system]
2020
requires = ["hatchling"]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from setuptools import setup
22

3-
setup(name="aoc-2023", version="0.1", description="AOC 2023", packages=["src/aoc_2023"])
3+
setup(name="advent-of-code-python", version="0.1", description="advent-of-code-python", packages=["src/advent_of_code"])
File renamed without changes.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import os
2+
import argparse
3+
4+
5+
TEMPLATES_PATH = os.path.join(".", "src", "advent_of_code", "scripts", "templates")
6+
7+
8+
def parse_input_args():
9+
parser = argparse.ArgumentParser()
10+
parser.add_argument("--day", type=str, required=True)
11+
parser.add_argument("--year", type=str, required=True)
12+
args = parser.parse_args()
13+
return args
14+
15+
16+
def read_template(template_file_path) -> None:
17+
with open(template_file_path, "r") as template_file:
18+
return template_file.read()
19+
20+
21+
def write_template(template: str, write_path: str) -> None:
22+
# if os.path.exists(write_path):
23+
# print(f"File {write_path} already exists! Not writing.")
24+
# else:
25+
with open(write_path, "w") as f:
26+
f.write(template)
27+
28+
29+
def generate_file(template_file_path, input_day, input_year, output_file_path):
30+
try:
31+
# Read the template content from the file
32+
template_content = read_template(template_file_path)
33+
except FileNotFoundError:
34+
print(f"Template file '{template_file_path}' not found.")
35+
exit(1)
36+
37+
# Replace placeholder with the provided integer input
38+
modified_content = template_content.replace("{day}", str(input_day)).replace(
39+
"{year}", str(input_year)
40+
)
41+
# Generate the Python file from the template
42+
write_template(modified_content, output_file_path)
43+
print(f"Python file '{output_file_path}' generated successfully.")
44+
45+
46+
if __name__ == "__main__":
47+
args = parse_input_args()
48+
input_day = args.day
49+
input_year = args.year
50+
51+
day_file_dest = os.path.join(
52+
"src", "advent_of_code", f"year_{input_year}", f"days/{input_day}.py"
53+
)
54+
template_path = os.path.join(TEMPLATES_PATH, "days_template.txt")
55+
generate_file(template_path, input_day, input_year, day_file_dest)
56+
57+
solvers_file_dest = os.path.join(
58+
"src",
59+
"advent_of_code",
60+
f"year_{input_year}",
61+
f"solvers/day_{input_day}_solvers.py",
62+
)
63+
template_path = os.path.join(TEMPLATES_PATH, "solvers_template.txt")
64+
generate_file(template_path, input_day, input_year, solvers_file_dest)
65+
66+
tests_file_dest = os.path.join(
67+
"tests", f"year_{input_year}", f"test_day_{input_day}_solvers.py"
68+
)
69+
template_path = os.path.join(TEMPLATES_PATH, "tests_template.txt")
70+
generate_file(template_path, input_day, input_year, tests_file_dest)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from advent_of_code.year_{year}.solvers.day_{day}_solvers import (
2+
solve_day_{day},
3+
)
4+
from advent_of_code.utils.input_handling import read_input, parse_args
5+
6+
7+
def main():
8+
args = parse_args()
9+
input = read_input(args.input_file)
10+
result_part_1, result_part_2 = solve_day_{day}(input)
11+
print(
12+
f"Day {day}: "
13+
f" Result for part 1 is {result_part_1}. "
14+
f" Result for part 2 is {result_part_2}. "
15+
)
16+
17+
18+
if __name__ == "__main__":
19+
main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def solve_day_{day}(input):
2+
part_1, part_2 = do_something(input)
3+
return (part_1, part_2)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pytest
2+
from advent_of_code.year_{year}.solvers.day_{day}_solvers import (
3+
solve_day_{day},
4+
)
5+
6+
7+
@pytest.fixture
8+
def day_{day}_test_input():
9+
return []
10+
11+
12+
@pytest.fixture
13+
def day_{day}_expected_output():
14+
return []

0 commit comments

Comments
 (0)