Skip to content

Commit c4b0fb0

Browse files
committed
add run_all_solutions.py sscript
1 parent 1b294fa commit c4b0fb0

File tree

7 files changed

+59
-24
lines changed

7 files changed

+59
-24
lines changed

Makefile

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,8 @@ ruff:
1616
test:
1717
python3 -m pytest
1818

19-
# Run specific solutions for Advent of Code 2023
2019
solutions:
21-
python3 src/advent_of_code/year_2023/days/1.py --input_file inputs/2023/01.txt --part 1
22-
python3 src/advent_of_code/year_2023/days/1.py --input_file inputs/2023/01.txt --part 2
23-
python3 src/advent_of_code/year_2023/days/2.py --input_file inputs/2023/02.txt
24-
python3 src/advent_of_code/year_2023/days/3.py --input_file inputs/2023/03.txt
25-
python3 src/advent_of_code/year_2023/days/4.py --input_file inputs/2023/04.txt
26-
python3 src/advent_of_code/year_2023/days/6.py --input_file inputs/2023/06.txt
20+
python3 scripts/run_all_solutions.py
2721

2822
.PHONY: new-day-skeleton-files-from-template
2923
new-day-skeleton-files-from-template:

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()

run_day.py renamed to scripts/run_day.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ def main():
2626
input_file = f"inputs/year_{args.year}/{day_zero_padded_str}.dat"
2727

2828
day_module = f"advent_of_code.year_{args.year}.day_{day_zero_padded_str}"
29-
print(day_module)
3029

3130
try:
3231
# Dynamically import the module for the specified day

src/advent_of_code/year_2023/day_02.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,11 @@ def calculate_game_power(max_colour_1, max_colour_2, max_colour_3):
5656
return max_colour_1 * max_colour_2 * max_colour_3
5757

5858

59-
def main():
60-
args = parse_args()
61-
input = read_input(args.input_file)
59+
def main(input_file):
60+
input = read_input(input_file)
6261
part_1_solution, part_2_solution = solve_day_2(input)
63-
print(
64-
f"Day 2: Part 1 solution is {part_1_solution}."
65-
f"Part 2 solution is {part_2_solution}."
66-
)
62+
print(f"Day 2: Part 1 solution is {part_1_solution}.")
63+
print(f"Part 2 solution is {part_2_solution}.")
6764

6865

6966
if __name__ == "__main__":

src/advent_of_code/year_2023/day_03.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,8 @@ def solve_day_3(input) -> int:
194194

195195

196196

197-
def main():
198-
args = parse_args()
199-
input = read_input(args.input_file)
197+
def main(input_file):
198+
input = read_input(input_file)
200199
result_part_1, result_part_2 = solve_day_3(input)
201200
print(
202201
f"Day 3: The sum of part numbers is {result_part_1}. "

src/advent_of_code/year_2023/day_04.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ def solve_day_4(input) -> int:
7979
return (total_score, n_scratchcards)
8080

8181

82-
def main():
83-
args = parse_args()
84-
input = read_input(args.input_file)
82+
def main(input_file):
83+
input = read_input(input_file)
8584
result_part_1, result_part_2 = solve_day_4(input)
8685
print(
8786
f"Day 4: "

src/advent_of_code/year_2023/day_06.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ def solve_day_6(input):
6363
return (races_part_1.solve(), races_part_2.solve())
6464

6565

66-
def main():
67-
args = parse_args()
68-
input = read_input(args.input_file)
66+
def main(input_file):
67+
input = read_input(input_file)
6968
result_part_1, result_part_2 = solve_day_6(input)
7069
print(
7170
f"Day 6: "

0 commit comments

Comments
 (0)