Code formatting

This commit is contained in:
akp 2023-12-16 01:31:16 +00:00
parent 0479d1d4d2
commit bfe2450ecc
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
2 changed files with 63 additions and 26 deletions

41
aoc
View file

@ -100,7 +100,9 @@ def run_command(args: list[str], stdin=None, cache=True) -> tuple[int, str]:
def format_result(v: str) -> str:
if len(v) == len("".join(filter(lambda x: x.isalpha() or x.isdigit() or x == "-", v))):
if len(v) == len(
"".join(filter(lambda x: x.isalpha() or x.isdigit() or x == "-", v))
):
return v
return repr(v)
@ -145,7 +147,9 @@ def run_part(command: list[str], label: str, spec: dict[str, str | BufferedReade
print(formatted_result_str)
def get_runner_command(file_name: str) -> tuple[list[str], Optional[Callable[[], None]]]:
def get_runner_command(
file_name: str,
) -> tuple[list[str], Optional[Callable[[], None]]]:
"""
Builds a solution using `command` then returns a path to the executable.
"""
@ -156,15 +160,17 @@ def get_runner_command(file_name: str) -> tuple[list[str], Optional[Callable[[],
raise SystemExit(1)
(runner_build, runner_run) = RUNNERS[file_extension]
if runner_build is None:
if runner_run is not None:
return runner_run + [file_name]
print(f"No build or run command specified for runner {file_extension}")
raise SystemExit(1)
if runner_run is not None and runner_build is not None:
print(f"Build command and run command specified for {file_extension} - cannot determine path forwards.")
print(
f"Build command and run command specified for {file_extension} - cannot determine path forwards."
)
raise SystemExit(1)
command = runner_build + [file_name]
@ -177,7 +183,7 @@ def get_runner_command(file_name: str) -> tuple[list[str], Optional[Callable[[],
raise SystemExit(1)
fpstr = fpath.decode().strip()
return [fpstr], lambda: os.unlink(fpstr)
class CLI(object):
@staticmethod
@ -312,7 +318,9 @@ class CLI(object):
cmd, cleanup = get_runner_command(fpath)
benchmark_file = Path(CHALLENGES_DIR) / challenge_dir.parts[1] / "benchmarks.jsonl"
benchmark_file = (
Path(CHALLENGES_DIR) / challenge_dir.parts[1] / "benchmarks.jsonl"
)
benchmark_fd = open(benchmark_file, "a")
for part in ["1", "2"]:
@ -328,12 +336,25 @@ class CLI(object):
input_file.seek(0)
durs.append(run_duration)
mi, mx, avg = min(durs), max(durs), sum(durs)/len(durs)
mi, mx, avg = min(durs), max(durs), sum(durs) / len(durs)
json.dump({"day": int(challenge_dir.parts[-1].split("-")[0]), "part": int(part), "runner": file_extension, "min": mi, "max": mx, "avg": avg, "n": n}, benchmark_fd)
json.dump(
{
"day": int(challenge_dir.parts[-1].split("-")[0]),
"part": int(part),
"runner": file_extension,
"min": mi,
"max": mx,
"avg": avg,
"n": n,
},
benchmark_fd,
)
benchmark_fd.write("\n")
print(f"Part {part}: min {round(mi, 4)} seconds, max {round(mx, 4)} seconds, avg {round(avg, 4)}")
print(
f"Part {part}: min {round(mi, 4)} seconds, max {round(mx, 4)} seconds, avg {round(avg, 4)}"
)
benchmark_fd.close()
input_file.close()

View file

@ -8,14 +8,17 @@ import sys
from typing import Optional
DIGIT_FONT = [[[(True if char == "x" else False) for char in line] for line in block.splitlines()] for block in open("digits.txt").read().split("\n\n")]
DIGIT_FONT = [
[[(True if char == "x" else False) for char in line] for line in block.splitlines()]
for block in open("digits.txt").read().split("\n\n")
]
background_colour = (0, 0, 0) # black
stationary_colour = (190, 52, 58) # red
falling_colour = (50, 49, 49) # grey
scan_colour = (52, 190, 58) # green
alt_scan_colour = (24, 77, 191) # blue
letter_colour = (255, 255, 255) # white
background_colour = (0, 0, 0) # black
stationary_colour = (190, 52, 58) # red
falling_colour = (50, 49, 49) # grey
scan_colour = (52, 190, 58) # green
alt_scan_colour = (24, 77, 191) # blue
letter_colour = (255, 255, 255) # white
frame_dir = Path("frames")
os.mkdir(frame_dir)
@ -24,7 +27,13 @@ counter = 0
frame_number = 0
scale_factor = 4
def draw_frame(platform: tuple[str], highlight_y: Optional[int] = None, allow_skip: bool = True, number: Optional[int] = None):
def draw_frame(
platform: tuple[str],
highlight_y: Optional[int] = None,
allow_skip: bool = True,
number: Optional[int] = None,
):
global frame_number, counter
counter += 1
@ -40,7 +49,7 @@ def draw_frame(platform: tuple[str], highlight_y: Optional[int] = None, allow_sk
for y, line in enumerate(platform):
for x, char in enumerate(line):
c = background_colour
if char == "#":
c = stationary_colour
if char == "O":
@ -63,15 +72,19 @@ def draw_frame(platform: tuple[str], highlight_y: Optional[int] = None, allow_sk
for xd, putpix in enumerate(line):
if putpix:
img.putpixel((pos_x + xd, pos_y + yd), letter_colour)
pos_x += 7 # 5 pixel wide font + 2 pixel gap
pos_x += 7 # 5 pixel wide font + 2 pixel gap
img = img.resize((x*scale_factor, y*scale_factor), resample=Image.NEAREST)
img.save(frame_dir/f"{str(frame_number).zfill(8)}.png")
img = img.resize((x * scale_factor, y * scale_factor), resample=Image.NEAREST)
img.save(frame_dir / f"{str(frame_number).zfill(8)}.png")
frame_number += 1
def modtilt(platform: tuple[str], direction: main.TiltDirection, allow_skip = True, partial = True) -> tuple[str]:
needs_flip = direction == main.TiltDirection.North or direction == main.TiltDirection.South
def modtilt(
platform: tuple[str], direction: main.TiltDirection, allow_skip=True, partial=True
) -> tuple[str]:
needs_flip = (
direction == main.TiltDirection.North or direction == main.TiltDirection.South
)
if direction == main.TiltDirection.North or direction == main.TiltDirection.South:
platform = main.flip_platform(platform)
@ -93,7 +106,10 @@ def modtilt(platform: tuple[str], direction: main.TiltDirection, allow_skip = Tr
changes = True
res[i] = after
if (partial and not changes) or not partial:
draw_frame(res if not needs_flip else main.flip_platform(res), allow_skip=allow_skip)
draw_frame(
res if not needs_flip else main.flip_platform(res),
allow_skip=allow_skip,
)
if direction == main.TiltDirection.North or direction == main.TiltDirection.South:
res = main.flip_platform(res)
@ -155,4 +171,4 @@ for y, line in enumerate(platform):
draw_frame(platform, highlight_y=y, number=acc)
for i in range(20):
draw_frame(platform, number=acc, allow_skip=False)
draw_frame(platform, number=acc, allow_skip=False)