Skip to content

Commit abaf413

Browse files
committed
Reorganize input files and solution files
1 parent 651988b commit abaf413

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
__pycache__
22
data/
3+
inputs/
4+
solutions/
35
*/input_data.py
46
.env
57
codingquest/2022/input/

pylib/aoc.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,17 +695,31 @@ def site(self) -> site.Website:
695695
@property
696696
def data_file(self) -> pathlib.Path:
697697
"""Return the path to today's data file."""
698-
return (self.data_dir / f'{self.day:02d}').with_suffix('.txt')
698+
return (self.data_dir / f'{self.year}.{self.day:02d}.txt')
699699

700700
@property
701701
def data_dir(self) -> pathlib.Path:
702-
"""Return the directory named "data" with the inputs."""
702+
"""Return the directory named "inputs" with the inputs."""
703703
for p in pathlib.Path(__file__).parents:
704-
d = p / 'data'
704+
d = p / 'inputs'
705705
if d.exists():
706706
return d
707707
raise RuntimeError('No data dir found')
708708

709+
@property
710+
def solutions_file(self) -> pathlib.Path:
711+
"""Return the path to today's data file."""
712+
return (self.solutions_dir / f'{self.year}.txt')
713+
714+
@property
715+
def solutions_dir(self) -> pathlib.Path:
716+
"""Return the directory named "solutions" with the solutions."""
717+
for p in pathlib.Path(__file__).parents:
718+
d = p / 'solutions'
719+
if d.exists():
720+
return d
721+
raise RuntimeError('No solution dir found')
722+
709723
@property
710724
def day(self) -> int:
711725
"""Return the day of this class."""
@@ -830,7 +844,7 @@ def solve(self, input_file: Optional[str]) -> None:
830844

831845
def check(self, input_file: Optional[str] = None) -> None:
832846
"""Check the generated solutions match the contents of solutions.txt."""
833-
lines = (self.data_dir.parent / 'solutions').with_suffix('.txt').read_text().strip().split('\n')
847+
lines = self.solutions_file.read_text().strip().split('\n')
834848
solution = None
835849
for line in lines:
836850
parts = line.split()

runner.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,34 @@ def build(klass, year: Optional[int], day: Optional[int], watch: bool, timeout:
101101

102102
@property
103103
def base(self):
104-
return pathlib.Path(__file__).parent / str(self.year)
104+
return pathlib.Path(__file__).parent
105+
106+
@property
107+
def year_path(self) -> pathlib.Path:
108+
return self.base / f"{self.year}"
109+
110+
@property
111+
def code_path(self) -> pathlib.Path:
112+
return self.year_path / f"d{day:02}.py"
113+
114+
@property
115+
def solution_path(self) -> pathlib.Path:
116+
return self.base / f"solutions/{self.year}.txt"
105117

106118
def from_template(self, day: int) -> None:
107119
"""Create a new exercise file from template."""
108-
filename = self.base / f"d{day:02}.py"
120+
filename = self.code_path
109121
if filename.exists():
110122
print(f"{filename.name} already exists")
111123
return
112-
template_file = self.base / "../shared/tmpl.py"
124+
template_file = self.base / "shared/tmpl.py"
113125
template = string.Template(template_file.read_text())
114126
website = site.Website(self.year, day)
115127
out = template.substitute(
116128
day=f"{day:02}",
117129
sample=website.codeblocks(),
118130
title=website.title().strip("- "),
119131
)
120-
filename = self.base / f"d{day:02}.py"
121132
filename.write_text(out)
122133
filename.chmod(0o700)
123134

@@ -127,7 +138,7 @@ def december(self, timeout: int) -> None:
127138
start = datetime.datetime(year, 11, 30, tzinfo=EST)
128139
end = datetime.datetime(year, 12, 25, 1, tzinfo=EST)
129140
while start < self.now() < end:
130-
solved = [int(line.split()[0]) for line in (self.base / "solutions.txt").read_text().splitlines()]
141+
solved = [int(line.split()[0]) for line in self.solution_path.read_text().splitlines()]
131142
if self.now().day in solved:
132143
print("Wait for tomorrow's problem to start and solve it.")
133144
self.wait_solve(timeout)
@@ -188,7 +199,7 @@ def live_solve(self) -> None:
188199
else:
189200
# Watch the file.
190201
inotify = inotify_simple.INotify()
191-
inotify.add_watch(self.base, inotify_simple.flags.CLOSE_WRITE)
202+
inotify.add_watch(self.year_path, inotify_simple.flags.CLOSE_WRITE)
192203
while events := inotify.read():
193204
if not any(i.name == f"d{day:02}.py" for i in events):
194205
continue
@@ -251,7 +262,7 @@ def live_solve(self) -> None:
251262
if not solutions:
252263
solutions = {part: obj.run_solver(part, raw_data) for part in (1, 2)}
253264
inotify = inotify_simple.INotify()
254-
inotify.add_watch(self.base, inotify_simple.flags.CLOSE_WRITE)
265+
inotify.add_watch(self.year_path, inotify_simple.flags.CLOSE_WRITE)
255266

256267
stop_at = self.now() + datetime.timedelta(hours=6)
257268
while self.now() < stop_at:
@@ -273,7 +284,7 @@ def live_solve(self) -> None:
273284
def read_solutions(self) -> dict[int, dict[int, int | str]]:
274285
"""Return solutions from file."""
275286
solutions: dict[int, dict[int, int | str]] = {}
276-
for line in (self.base / "solutions.txt").read_text().splitlines():
287+
for line in self.solution_path.read_text().splitlines():
277288
if not line:
278289
continue
279290
parts = line.split()
@@ -319,8 +330,7 @@ def update_solutions(self, day: int, solutions: Optional[dict[int, int | str]] =
319330
for line_day in sorted(existing)
320331
]
321332

322-
solution_file = self.base / "solutions.txt"
323-
solution_file.write_text("\n".join(lines) + "\n")
333+
self.solution_path.write_text("\n".join(lines) + "\n")
324334

325335

326336
@click.command()

0 commit comments

Comments
 (0)