Skip to content

Commit 67ba48d

Browse files
authored
Refactor 2024 (#15)
* refactor 2023 code * convert to dat file xt * renaming * fix input file typo * refactoring * remove txt files * refacotr inputs and tessts filenames * fix filenaming * add run_all_solutions.py sscript * ruff
1 parent e0e49c4 commit 67ba48d

30 files changed

+191
-124
lines changed

Makefile

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
11
.PHONY: all clean black flake test solutions
22

33
# Default target
4-
all: black flake test solutions clean
4+
all: ruff test solutions clean
55

66
# Clean temporary and generated files
77
clean:
88
find . \( -type f -name '*.pyc' -or -type d -name '__pycache__' \) -delete
99
find . \( -type d -name '.eggs' -or -type d -name '*.egg-info' -or -type d -name '.pytest_cache' \) | xargs rm -rf
1010

1111
# Format code using black
12-
black:
13-
black ./src ./tests
14-
15-
# Run flake8 for linting
16-
flake:
17-
flake8 ./src ./tests
12+
ruff:
13+
ruff check .
1814

1915
# Run tests
2016
test:
2117
python3 -m pytest
2218

23-
# Run specific solutions for Advent of Code 2023
2419
solutions:
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
20+
python3 scripts/run_all_solutions.py
3121

3222
.PHONY: new-day-skeleton-files-from-template
3323
new-day-skeleton-files-from-template:
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
black==23.12.1
2-
flake8==6.1.0
1+
ruff==0.1.9
32
numpy==1.26.2
43
pytest==7.4.3
54
setuptools==68.2.2

scripts/run_all_solutions.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os
2+
import re
3+
import subprocess
4+
5+
BASE_DIR = "src/advent_of_code"
6+
YEARS_DIR = os.path.join(BASE_DIR, "year_")
7+
RUN_DAY_SCRIPT = "scripts/run_day.py"
8+
INPUTS_DIR = "inputs"
9+
10+
def discover_and_run_solutions():
11+
# Regex to match "day_<number>.py"
12+
day_pattern = re.compile(r"day_(\d{2})\.py")
13+
14+
for year in sorted(os.listdir(BASE_DIR)):
15+
if year.startswith("year_"):
16+
year_path = os.path.join(BASE_DIR, year)
17+
year_number = year.split("_")[1]
18+
19+
# Look for days in the "days" directory
20+
days_dir = os.path.join(year_path)
21+
for file in sorted(os.listdir(days_dir)):
22+
match = day_pattern.match(file)
23+
if match:
24+
day_number = match.group(1)
25+
26+
# Build input file path
27+
input_file = os.path.join(INPUTS_DIR, f"year_{year_number}", f"{day_number}.dat")
28+
if not os.path.exists(input_file):
29+
print(f"Input file for {year_number} Day {day_number} not found, skipping.")
30+
continue
31+
32+
# Run the solution using run_day.py
33+
try:
34+
print(f"Running {year_number} Day {day_number}...")
35+
subprocess.run(
36+
[
37+
"python3", RUN_DAY_SCRIPT,
38+
"--year", year_number,
39+
"--day", str(int(day_number)), # Remove leading zero for argument
40+
],
41+
check=True
42+
)
43+
print("\n")
44+
except subprocess.CalledProcessError as e:
45+
print(f"Error running {year_number} Day {day_number}: {e}")
46+
47+
if __name__ == "__main__":
48+
discover_and_run_solutions()

scripts/run_day.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import importlib
2+
import sys
3+
import argparse
4+
5+
def main():
6+
parser = argparse.ArgumentParser(description="Run a specific Advent of Code solution.")
7+
# parser.add_argument(
8+
# "--input_file",
9+
# required=True,
10+
# type=str,
11+
# help="Path to the input file for the day's solution."
12+
# )
13+
parser.add_argument(
14+
"--year",
15+
required=True,
16+
type=int,
17+
)
18+
parser.add_argument(
19+
"--day",
20+
required=True,
21+
type=str,
22+
)
23+
24+
args = parser.parse_args()
25+
day_zero_padded_str = str(args.day).zfill(2)
26+
input_file = f"inputs/year_{args.year}/{day_zero_padded_str}.dat"
27+
28+
day_module = f"advent_of_code.year_{args.year}.day_{day_zero_padded_str}"
29+
30+
try:
31+
# Dynamically import the module for the specified day
32+
module = importlib.import_module(day_module)
33+
# Run the solution (assumes each solver module has a `main()` function)
34+
if hasattr(module, "main"):
35+
module.main(input_file)
36+
else:
37+
print("man")
38+
print(f"The module {day_module} does not have a 'main(input_file)' function.")
39+
except ModuleNotFoundError:
40+
print(f"Could not find module: {day_module}")
41+
sys.exit(1)
42+
43+
if __name__ == "__main__":
44+
main()

0 commit comments

Comments
 (0)