Solve day 2 part 1

This commit is contained in:
Gabriel Augendre 2022-12-02 14:08:51 +01:00
parent cd3e1ecbc5
commit ec3209ef08
4 changed files with 2643 additions and 3 deletions

View file

@ -0,0 +1,134 @@
from __future__ import annotations
import dataclasses
import enum
import pytest
def main(filename: str, expected_part_1: int = None, expected_part_2: int = None):
print(f"\n+ Running on {filename}")
with open(filename) as f:
data = f.read().strip().split("\n")
data = parse_data(data)
solution_part_1 = solve_part_1(data)
print(f"1. Found {solution_part_1}")
if expected_part_1:
assert expected_part_1 == solution_part_1
solution_part_2 = solve_part_2(data)
print(f"2. Found {solution_part_2}")
if expected_part_2:
assert expected_part_2 == solution_part_2
class Shape(enum.IntEnum):
ROCK = 1
PAPER = 2
SCISSORS = 3
@classmethod
def from_input(cls, letter: str) -> Shape:
return {
"A": cls.ROCK,
"B": cls.PAPER,
"C": cls.SCISSORS,
"X": cls.ROCK,
"Y": cls.PAPER,
"Z": cls.SCISSORS,
}[letter]
@dataclasses.dataclass
class Round:
opponent: Shape
me: Shape
@property
def value(self) -> int:
return self.me.value + self.score
@property
def score(self) -> int:
if self.victory:
return 6
elif self.draw:
return 3
else:
return 0
@property
def victory(self) -> bool:
return (self.me.value - self.opponent.value % 3) == 1
@property
def draw(self) -> bool:
return self.opponent == self.me
DataType = list[Round]
def parse_data(data: list[str]) -> DataType:
rounds = []
for round in data: # noqa: A001
opponent, me = round.split(" ")
rounds.append(Round(Shape.from_input(opponent), Shape.from_input(me)))
return rounds
def solve_part_1(data: DataType) -> int:
return sum([round.value for round in data]) # noqa: A001
def solve_part_2(data: DataType) -> int:
return 0
@pytest.mark.parametrize(
"opponent,me",
[
(Shape.ROCK, Shape.PAPER),
(Shape.PAPER, Shape.SCISSORS),
(Shape.SCISSORS, Shape.ROCK),
],
)
def test_round_victory(opponent, me):
round = Round(opponent, me) # noqa: A001
assert round.victory
assert not round.draw
@pytest.mark.parametrize(
"opponent,me",
[
(Shape.ROCK, Shape.ROCK),
(Shape.PAPER, Shape.PAPER),
(Shape.SCISSORS, Shape.SCISSORS),
],
)
def test_round_draw(opponent, me):
round = Round(opponent, me) # noqa: A001
assert not round.victory
assert round.draw
@pytest.mark.parametrize(
"opponent,me",
[
(Shape.PAPER, Shape.ROCK),
(Shape.SCISSORS, Shape.PAPER),
(Shape.ROCK, Shape.SCISSORS),
],
)
def test_round_defeat(opponent, me):
round = Round(opponent, me) # noqa: A001
assert not round.victory
assert not round.draw
if __name__ == "__main__":
main("inputs/day02-test1", expected_part_1=15)
main("inputs/day02")

2500
2022/inputs/day02 Normal file

File diff suppressed because it is too large Load diff

3
2022/inputs/day02-test1 Normal file
View file

@ -0,0 +1,3 @@
A Y
B X
C Z

View file

@ -16,15 +16,18 @@ def main(filename: str, expected_part_1: int = None, expected_part_2: int = None
assert expected_part_2 == solution_part_2 assert expected_part_2 == solution_part_2
def parse_data(data: list[str]): DataType = list[str]
def parse_data(data: list[str]) -> DataType:
return data return data
def solve_part_1(data) -> int: def solve_part_1(data: DataType) -> int:
return 0 return 0
def solve_part_2(data) -> int: def solve_part_2(data: DataType) -> int:
return 0 return 0