add rockpaperscissors solution

This commit is contained in:
aaron
2022-12-03 13:21:31 +01:00
parent 32eb655c97
commit 4721bbd87f

View File

@@ -1,84 +1,50 @@
from aocd.models import Puzzle
from aocd import submit
def eval_score(game_round: list) -> int:
'''
calculate game score for single round
'''
# score map part a
scores_a = {
"A X": 4,
"A Y": 8,
"A Z": 3,
"B X": 1,
"B Y": 5,
"B Z": 9,
"C X": 7,
"C Y": 2,
"C Z": 6,
}
score = 0
player_a = game_round[1]
player_b = game_round[0]
# score map part b
scores_b = {
"A X": 3,
"A Y": 4,
"A Z": 8,
"B X": 1,
"B Y": 5,
"B Z": 9,
"C X": 2,
"C Y": 6,
"C Z": 7,
}
match player_b:
case 'A': # rock
if player_a == 'X': # rock
score += 3
score += 1
if player_a == 'Y': # paper
score += 6
score += 2
if player_a == 'Z': # scissors
score += 0
score += 3
case 'B': # paper
if player_a == 'X': # rock
score += 0
score += 1
if player_a == 'Y': # paper
score += 3
score += 2
if player_a == 'Z': # scissors
score += 6
score += 3
case 'C': # scissors
if player_a == 'X': # rock
score += 6
score += 1
if player_a == 'Y': # paper
score += 0
score += 2
if player_a == 'Z': # scissors
score += 3
score += 3
return score
def manipulate_round(game_round: list) -> list:
'''
manipulate round according to rule set
'''
move = ''
match game_round[0]:
case 'A': # rock
if game_round[1] == 'X': # lose
move = 'Z'
if game_round[1] == 'Y': # draw
move = 'X'
if game_round[1] == 'Z': # win
move = 'Y'
case 'B': # paper
if game_round[1] == 'X': # lose
move = 'X'
if game_round[1] == 'Y': # draw
move = 'Y'
if game_round[1] == 'Z': # win
move = 'Z'
case 'C': # scissors
if game_round[1] == 'X': # lose
move = 'Y'
if game_round[1] == 'Y': # draw
move = 'Z'
if game_round[1] == 'Z': # win
move = 'X'
return [game_round[0], move]
def eval_score(game: list, scores: dict) -> int:
"""
calculate the score of a game
"""
# map the move list to scores
score = [scores[move] for move in game]
# return the sum of all scores
return sum(score)
def parse_input(data: list) -> list:
'''
parses the input data and generates a list of elfs
'''
"""
parses the input data and generates a list of elfs
"""
# split move set into a list of rounds
return [ move.split() for move in data.split('\n')]
return [move for move in data.split("\n")]
if __name__ == "__main__":
# get puzzle and parse data
@@ -86,15 +52,11 @@ if __name__ == "__main__":
game = parse_input(puzzle.input_data)
# part a: calculate total score
total = 0
for game_round in game:
total += eval_score(game_round)
print(f'answer part a: total points -> {total}')
submit(total, part='a', day=2, year=2022)
total = eval_score(game, scores_a)
print(f"total points part a: {total}")
submit(total, part="a", day=2, year=2022)
# part b: calculate total score using the secret list
total = 0
for game_round in game:
total += eval_score(manipulate_round(game_round))
print(f'answer part b: total points -> {total}')
submit(total, part='b', day=2, year=2022)
total = eval_score(game, scores_b)
print(f"total points part b: {total}")
submit(total, part="b", day=2, year=2022)