Skip to content

Commit

Permalink
Day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Ted Cassirer committed Dec 2, 2023
1 parent a49d8c2 commit 9b9845f
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 15 deletions.
4 changes: 2 additions & 2 deletions aoc_cas/aoc2023/day1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from aoc_cas.util import test_with_examples
from aoc_cas.util import solve_with_examples


def part_a(data) -> int:
Expand Down Expand Up @@ -39,4 +39,4 @@ def part_b(data) -> int:


if __name__ == "__main__":
test_with_examples(2023, 1)
solve_with_examples(2023, 1)
45 changes: 45 additions & 0 deletions aoc_cas/aoc2023/day2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import dataclasses
from collections import defaultdict
from typing import Self

from aoc_cas.util import solve_with_examples


@dataclasses.dataclass(frozen=True)
class Round:
id: int
red: int = 0
blue: int = 0
green: int = 0

@classmethod
def from_data_line(cls, line: str) -> Self:
game_part, cube_part = line.split(": ")
game_id = int(game_part.split(" ")[1])

most_seen: dict[str, int] = defaultdict(int)
for cube_set in cube_part.split("; "):
for draw in cube_set.split(", "):
count, color = draw.split(" ")
most_seen[color] = max(most_seen[color], int(count))

return Round(game_id, **most_seen)


def part_a(data: str) -> int:
result = 0
for round in map(Round.from_data_line, data.splitlines()):
if round.red <= 12 and round.green <= 13 and round.blue <= 14:
result += round.id
return result


def part_b(data: str) -> int:
result = 0
for round in map(Round.from_data_line, data.splitlines()):
result += round.blue * round.green * round.red
return result


if __name__ == "__main__":
solve_with_examples(year=2023, day=2)
12 changes: 6 additions & 6 deletions aoc_cas/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def main():
def solve(year: int, day: int, submit: bool) -> None:
puzzle = Puzzle(year, day)
log.info(f"--- {year} Day {day}: {puzzle.title} ---")
part_1_result, part_2_result = plugin(year, day)
log.info(f"PartA: {part_1_result}")
log.info(f"PartB: {part_2_result}")
part_a_result, part_b_result = plugin(year=year, day=day, data=puzzle.input_data)
log.info(f"PartA: {part_a_result}")
log.info(f"PartB: {part_b_result}")
if submit:
puzzle.answer_a = part_1_result
puzzle.answer_2 = part_2_result
puzzle.answer_a = part_a_result
puzzle.answer_b = part_b_result


@main.command()
Expand All @@ -42,7 +42,7 @@ def init_challenge(year: int, day: int) -> None:
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))
python_file.write_text(challenge_template.format(day=day, year=year))

test_folder = Path.cwd() / f"tests/fixtures/{year}"
test_folder.mkdir(exist_ok=True)
Expand Down
6 changes: 3 additions & 3 deletions aoc_cas/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def load_module(year: int, day: int):
return importlib.import_module(module_name)


def test_with_examples(year: int, day: int) -> None:
def solve_with_examples(year: int, day: int) -> None:
mod = load_module(year, day)
puzzle = Puzzle(year, day)
print(f"Testing example data")
Expand All @@ -19,6 +19,6 @@ def test_with_examples(year: int, day: int) -> None:
correct = part_a_result == example.answer_a
print(f"{'❌✅'[correct]} [Part A] Actual: {part_a_result} - Expected: {example.answer_a}")
if example.answer_b is not None:
part_b_result = mod.part_b(example.input_data)
correct = part_b_result == example.answer_a
part_b_result = str(mod.part_b(example.input_data))
correct = part_b_result == example.answer_b
print(f"{'❌✅'[correct]} [Part B] Actual: {part_b_result} - Expected: {example.answer_b}\n")
8 changes: 4 additions & 4 deletions challenge_template.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from aoc_cas.util import test_with_examples
from aoc_cas.util import solve_with_examples


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


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

if __name__ == "__main__":
test_with_examples({}, {})
solve_with_examples(year={year}, day={day})
7 changes: 7 additions & 0 deletions tests/fixtures/2023/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
8
2286

0 comments on commit 9b9845f

Please sign in to comment.