-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjolt
More file actions
executable file
Β·207 lines (168 loc) Β· 6.02 KB
/
Copy pathjolt
File metadata and controls
executable file
Β·207 lines (168 loc) Β· 6.02 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python3
from functools import partial
import click
from colorama import Fore, Style
from environs import Env
from common.aoc import fetch_calendar, fetch_example, fetch_user_input
from common.commands.benchmark import (
print_overall_summary,
print_year_summary,
process_day_benchmarks,
)
from common.date import current_day, current_year
from common.file import create_day, save_to_day
from common.metadata import Metadata, read_day_metadata, save_day_metadata
from common.runner import run_day
from common.template import Language, get_template
@click.group(invoke_without_command=True)
@click.pass_context
def cli(ctx) -> None:
if ctx.invoked_subcommand is None:
puzzle()
@cli.command()
@click.option(
"--year", "-y", type=int, help="Year of the puzzle", default=current_year()
)
@click.option("--day", "-d", type=int, help="Day of the puzzle", default=current_day())
@click.option(
"--language",
"-l",
type=click.Choice([lang.name for lang in Language]),
default=Language.PYTHON.name,
callback=lambda ctx, param, value: Language.from_name(value),
)
@click.option(
"--separate",
"-s",
is_flag=True,
help="Separate part 1 and 2",
default=False,
)
def puzzle(year: int, day: int, language: Language, separate: bool) -> None:
"""Create a new puzzle solution template."""
create_day(year, day)
save_day_metadata(
year,
day,
Metadata(
language,
[f"solution.{language.extension}"]
if not separate
else [f"part_1.{language.extension}", f"part_2.{language.extension}"],
),
)
save = partial(save_to_day, year, day)
save("input.txt", fetch_user_input(year, day))
try:
save("e1.input.txt", fetch_example(year, day, 1))
except ValueError:
pass
if separate:
save(f"part_1.{language.extension}", get_template(language, "part_1"))
save(f"part_2.{language.extension}", get_template(language, "part_2"))
else:
save(f"solution.{language.extension}", get_template(language), False)
repeat = 30 if day < 10 else 31
print(f"{Fore.GREEN}{'*' * repeat}")
print(f"{Fore.GREEN}π Advent of Code {year} - Day {day} {Style.RESET_ALL}")
print(f"{Fore.GREEN}{'*' * repeat}")
print()
print(
f"{Fore.BLUE}π Look {Style.BRIGHT}{Fore.YELLOW}https://adventofcode.com/{year}/day/{day}{Style.RESET_ALL}{Fore.BLUE} for more info {Style.RESET_ALL}"
)
print(
f"{Fore.RED}Don't forget to check the problem statement and understand it well! {Style.RESET_ALL} π‘"
)
print(
f"{Fore.CYAN}If you finish it or if you're stuck, don't forget to check out other solutions on the solution megathread on Reddit: {Style.BRIGHT}{Fore.YELLOW}https://www.reddit.com/r/adventofcode{Style.RESET_ALL}"
)
print(f"{Fore.YELLOW}Happy Coding! {Style.RESET_ALL} π¨βπ»")
@cli.command()
@click.option(
"--year", "-y", type=int, help="Year of the puzzle", default=current_year()
)
@click.option("--day", "-d", type=int, help="Day of the puzzle", default=current_day())
def run(year: int, day: int) -> None:
"""Run solution(s) for specified day and year."""
metadata = read_day_metadata(year, day)
for i, run in enumerate(run_day(year, day, metadata, benchmark=False)):
if i > 0:
click.echo(f"{Fore.LIGHTBLACK_EX}{'β' * 30}{Style.RESET_ALL}")
click.echo(
f"\n{Fore.CYAN}βΆ {Fore.LIGHTCYAN_EX}{run.beauty_func_name()} {Fore.LIGHTBLACK_EX}({run.file_name}){Style.RESET_ALL}"
)
if run.result is not None:
click.echo(
f"{Fore.WHITE}Result: {Fore.LIGHTGREEN_EX}{run.result}{Style.RESET_ALL}"
)
click.echo(
f"{Fore.WHITE}Time: {Fore.LIGHTMAGENTA_EX}{run.time_taken_formatted()}{Style.RESET_ALL}"
)
click.echo()
@cli.command()
@click.option(
"--year",
"-y",
type=int,
help="Year of the puzzle (defaults to current year)",
default=current_year(),
)
@click.option(
"--day",
"-d",
type=int,
help="Day of the puzzle (defaults to current day)",
default=current_day(),
)
@click.option(
"--all-days", "-ad", is_flag=True, help="Benchmark all days in specified year"
)
@click.option("--all-years", "-ay", is_flag=True, help="Benchmark all years")
def bench(year: int, day: int, all_days: bool, all_years: bool) -> None:
"""Benchmark solution(s) for specified day(s) and year(s)."""
years = range(2015, current_year() + 1) if all_years else [year]
days = range(1, 26) if all_days else [day]
total_results = []
for yr in years:
if len(years) > 1 or all_days:
click.echo(f"\n{Fore.YELLOW}Benchmarking Year {yr}{Style.RESET_ALL}")
year_results = []
for d in days:
try:
meta = read_day_metadata(yr, d)
runs = list(run_day(yr, d, meta, benchmark=True))
if runs:
day_results = process_day_benchmarks(yr, d, runs)
year_results.extend(day_results)
except Exception as e:
if not (all_days or all_years):
click.echo(
f"{Fore.RED}Error benchmarking Day {d}: {e}{Style.RESET_ALL}"
)
continue
if year_results:
total_results.extend(year_results)
if all_days:
print_year_summary(yr, year_results)
if all_years and total_results:
print_overall_summary(total_results)
@cli.command()
@click.option(
"--year",
"-y",
type=int,
help="Year of the advent of code (defaults to current year)",
default=current_year(),
)
def calendar(year: int) -> None:
"""Prints the calendar for the specified year."""
try:
print(fetch_calendar(year))
except Exception:
click.echo(
f"{Fore.RED}Calendar for year {year} is not available{Style.RESET_ALL}"
)
if __name__ == "__main__":
env = Env()
env.read_env()
cli()