Skip to content

Commit

Permalink
add 2023 (and run black)
Browse files Browse the repository at this point in the history
  • Loading branch information
crerwin committed Dec 1, 2023
1 parent 3ac72cc commit 9de2b51
Show file tree
Hide file tree
Showing 9 changed files with 1,148 additions and 5 deletions.
13 changes: 9 additions & 4 deletions advent/advent.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import click
import logging
from .day import Day
from .template import stubout_day
from .template import stubout_day, stubout_year

from advent import (
advent2015,
Expand All @@ -12,6 +12,7 @@
advent2020,
advent2021,
advent2022,
advent2023,
)

logger = logging.getLogger("advent")
Expand All @@ -29,6 +30,7 @@ def __init__(self):
self.days["2020"] = advent2020.days
self.days["2021"] = advent2021.days
self.days["2022"] = advent2022.days
self.days["2023"] = advent2023.days

def run_day(self, year, day, part):
yr = self.days[str(year)]
Expand Down Expand Up @@ -110,7 +112,10 @@ def show():


@advent.command()
@click.option("--year", "-y", prompt="Year: ", type=int)
@click.option("--day", "-d", prompt="Day: ", type=int)
@click.option("--year", "-y", required=True, type=int)
@click.option("--day", "-d", type=int)
def stubout(year: int, day: int):
stubout_day(year, day)
if day:
stubout_day(year, day)
else:
stubout_year(year)
2 changes: 1 addition & 1 deletion advent/advent2015/day7.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(self, input_connection=None):

def _get_output(self):
# bitwise compliment
return ~self.input + 2 ** 16
return ~self.input + 2**16

output = property(_get_output)

Expand Down
3 changes: 3 additions & 0 deletions advent/advent2023/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .day1 import Day1

days = {1: Day1}
39 changes: 39 additions & 0 deletions advent/advent2023/day1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from advent.day import Day


class Day1(Day):
year = 2023
day = 1

def _part1(self):
return self._get_calibration_sum(self.input().splitlines())

def _get_calibration_sum(self, lines: [str]) -> int:
if len(lines) < 1:
raise ValueError(f"No lines were passed: {lines}")

else:
sum = 0
for line in lines:
sum += self._get_calibration_values(line)
return sum

def _get_calibration_values(self, line: str) -> int:
return int(
self._get_calibration_digit(line) + self._get_calibration_digit(line, True)
)

def _get_calibration_digit(self, line: str, reverse: bool = False) -> str:
chars = list(line)

if reverse:
for char in chars[::-1]:
if char.isdigit():
return char

else:
for char in chars:
if char.isdigit():
return char

raise ValueError(f"Did not find a digit in line: {line}")
34 changes: 34 additions & 0 deletions advent/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,35 @@
logger = logging.getLogger("advent")


def _stubout_year_structure(year: int) -> None:
year_dir = f"advent/advent{year}"
year_inputs_dir = f"inputs/{year}"
init_file = f"{year_dir}/__init__.py"

logger.info(f"Stubbing out year structure for {year}.")
if not os.path.exists(year_dir):
os.mkdir(year_dir)
logger.info(f"{year_dir} created.")
else:
logger.info(f"{year_dir} already exists.")

if not os.path.exists(year_inputs_dir):
os.mkdir(year_inputs_dir)
logger.info(f"{year_inputs_dir} created.")
else:
logger.info(f"{year_inputs_dir} already exists.")

if not os.path.exists(init_file):
with open("advent/templates/__init__.py.j2", "r") as file:
init_template = Template(file.read())

with open(init_file, "w") as output_file:
output_file.write(init_template.render())
logger.info(f"{init_file} created.")
else:
logger.info(f"{init_file} already exists.")


def _stubout_day_code(year: int, day: int) -> None:
day_code_file = f"advent/advent{year}/day{day}.py"

Expand Down Expand Up @@ -62,10 +91,15 @@ def _create_input_file(year: int, day: int) -> None:


def stubout_day(year: int, day: int) -> None:
_stubout_year_structure(year)
_stubout_day_code(year, day)
_stubout_day_tests(year, day)
_create_input_file(year, day)

logger.info(
f"Templating complete. You must import the day inside advent/advent{year}/__init__.py"
)


def stubout_year(year: int) -> None:
_stubout_year_structure(year)
Empty file added advent/templates/__init__.py.j2
Empty file.
Loading

0 comments on commit 9de2b51

Please sign in to comment.