Skip to content

Commit

Permalink
Init challenge function added
Browse files Browse the repository at this point in the history
  • Loading branch information
Ted Cassirer committed Dec 1, 2023
1 parent 0864ca9 commit 2bb4346
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 80 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ poetry run pytest

## Run
```sh
poetry run aoccas -y 2021 -d 5 --submit
poetry run aoccas solve -y 2023 -d 1 --submit
```

## Initialize new challenge file
```shell
poetry run aoccas init-challenge -y 2023 -d 1
```

To submit the results you can add the `-s` flag
Expand All @@ -38,5 +43,5 @@ Instructions: [How to find the session id](https://github.com/wimglenn/advent-of
To run it using the aocd plugin you can run:

```sh
poetry run aoc -y 2021 -d 1
poetry run aoc -y 2023 -d 1
```
15 changes: 0 additions & 15 deletions aoc_cas/aoc2016/template.py

This file was deleted.

11 changes: 0 additions & 11 deletions aoc_cas/aoc2019/template.py

This file was deleted.

15 changes: 0 additions & 15 deletions aoc_cas/aoc2021/template.py

This file was deleted.

15 changes: 0 additions & 15 deletions aoc_cas/aoc2022/template.py

This file was deleted.

4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day1.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
def part1(data):
def part1(data) -> int:
result = 0
for line in data.splitlines():
digits = [int(char) for char in line if char.isdigit()]
result += digits[0] * 10 + digits[-1]
return result


def part2(data):
def part2(data) -> int:
digits = {
"1": 1,
"2": 2,
Expand Down
39 changes: 32 additions & 7 deletions aoc_cas/cli.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
import logging
from datetime import datetime
from pathlib import Path

import click
from aocd.models import Puzzle
from aocd.utils import AOC_TZ

from . import plugin
import os

log = logging.getLogger()

@click.command()

@click.group()
def main():
logging.basicConfig(format="%(message)s", level=logging.INFO)


@main.command()
@click.option("--year", "-y", default=datetime.now(tz=AOC_TZ).year, type=int)
@click.option("--day", "-d", default=datetime.now(tz=AOC_TZ).day, type=int)
@click.option("--submit", "-s", is_flag=True)
def run_one(year: int, day: int, submit: bool):
logging.basicConfig(format="%(message)s", level=logging.INFO)

def solve(year: int, day: int, submit: bool) -> None:
puzzle = Puzzle(year, day)
print(f"--- {year} Day {day}: {puzzle.title} ---")
log.info(f"--- {year} Day {day}: {puzzle.title} ---")
part_1_result, part_2_result = plugin(year, day)
print(f"Part1: {part_1_result}")
print(f"Part2: {part_2_result}")
log.info(f"Part1: {part_1_result}")
log.info(f"Part2: {part_2_result}")
if submit:
puzzle.answer_a = part_1_result
puzzle.answer_2 = part_2_result


@main.command()
@click.option("--year", "-y", default=datetime.now(tz=AOC_TZ).year, type=int)
@click.option("--day", "-d", default=datetime.now(tz=AOC_TZ).day, type=int)
def init_challenge(year: int, day: int) -> None:
year_folder = Path.cwd() / "aoc_cas" / f"aoc{year}"
challenge_template = (Path.cwd() / "challenge_template.txt").read_text()
if not year_folder.exists():
year_folder.mkdir()
year_folder.joinpath("__init__.py").touch()
python_file = year_folder / f"day{day}.py"
if not python_file.exists():
python_file.write_text(challenge_template.format(day, year))

test_folder = Path.cwd() / f"tests/fixtures/{year}"
test_folder.mkdir(exist_ok=True)
test_folder.joinpath(f"{day}.txt").touch()
14 changes: 14 additions & 0 deletions challenge_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def part1(data: str) -> int:
pass


def part2(data: str) -> int:
pass


if __name__ == "__main__":
from aocd import get_data

data = get_data(year={}, day={})
print(part1(data))
print(part2(data))
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ build-backend = "poetry.core.masonry.api"
line-length = 120

[tool.poetry.scripts]
aoccas = "aoc_cas.cli:run_one"
aoccas = "aoc_cas.cli:main"

[tool.poetry.plugins."adventofcode.user"]
aoccas = "aoc_cas:plugin"
Expand Down
14 changes: 2 additions & 12 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,19 @@
input_files = sorted(here.glob("fixtures/20*/*.txt"))


def path2id(input_file):
def path2id(input_file) -> str:
*pre, year, fname = input_file.parts
return f"{year} - {fname[:-4]}"


def remove_trailing_comments(lines):
while lines and (not lines[-1].strip() or lines[-1].startswith("#")):
lines.pop()
if len(lines):
lines[-1] = lines[-1].split("#")[0].strip()
if len(lines) > 1:
lines[-2] = lines[-2].split("#")[0].strip()


@pytest.mark.parametrize("input_file", input_files, ids=path2id)
def test_examples(input_file):
def test_examples(input_file) -> None:
*pre, year, fname = input_file.parts
year = int(year)
day = int(fname[:-4])
groups = input_file.read_text(encoding="utf-8").split("\n===\n")
for group in groups:
lines = group.splitlines()
remove_trailing_comments(lines)
if len(lines) < 3:
pytest.fail(f"test data {input_file} is malformed")
*lines, expected_answer_a, expected_answer_b = lines
Expand Down

0 comments on commit 2bb4346

Please sign in to comment.