day02 a bit more decoupled

This commit is contained in:
Ruediger Ludwig 2022-12-02 07:52:39 +01:00
parent 656df6deb6
commit 8f66f44335
2 changed files with 11 additions and 11 deletions

View file

@ -11,7 +11,7 @@ def part1(lines: Iterator[str]) -> int:
def part2(lines: Iterator[str]) -> int:
return sum(result.score(opponent)
return sum(result.player_shape(opponent).score(opponent)
for opponent, result in (Result.parse(line) for line in lines))
@ -187,9 +187,9 @@ class Result(Enum):
case 'Z': return Result.Win
case _: raise Exception(f"Unknown char : {char}")
def score(self, other: Shape) -> int:
""" The score we get by the given shape and the expected result """
def player_shape(self, other: Shape) -> Shape:
""" The shape the player must choose to get the expected result"""
match self:
case Result.Lose: return other.prev().score(other)
case Result.Draw: return other.score(other)
case Result.Win: return other.next().score(other)
case Result.Lose: return other.prev()
case Result.Draw: return other
case Result.Win: return other.next()

View file

@ -33,13 +33,13 @@ def test_round1():
def test_round2():
input = "A Y"
expected = 4
opponent, player = Result.parse(input)
assert player.score(opponent) == expected
expected = Shape.Rock
opponent, result = Result.parse(input)
assert result.player_shape(opponent) == expected
def test_round3():
input = "B X"
expected = 1
opponent, player = Result.parse(input)
assert player.score(opponent) == expected
opponent, result = Result.parse(input)
assert result.player_shape(opponent).score(opponent) == expected