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.models import Puzzle
from aocd import submit from aocd import submit
def eval_score(game_round: list) -> int: # score map part a
''' scores_a = {
calculate game score for single round "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 # score map part b
player_a = game_round[1] scores_b = {
player_b = game_round[0] "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: def eval_score(game: list, scores: dict) -> int:
''' """
manipulate round according to rule set calculate the score of a game
''' """
move = '' # map the move list to scores
match game_round[0]: score = [scores[move] for move in game]
case 'A': # rock # return the sum of all scores
if game_round[1] == 'X': # lose return sum(score)
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 parse_input(data: list) -> list: 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 # 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__": if __name__ == "__main__":
# get puzzle and parse data # get puzzle and parse data
@@ -86,15 +52,11 @@ if __name__ == "__main__":
game = parse_input(puzzle.input_data) game = parse_input(puzzle.input_data)
# part a: calculate total score # part a: calculate total score
total = 0 total = eval_score(game, scores_a)
for game_round in game: print(f"total points part a: {total}")
total += eval_score(game_round) submit(total, part="a", day=2, year=2022)
print(f'answer part a: total points -> {total}')
submit(total, part='a', day=2, year=2022)
# part b: calculate total score using the secret list # part b: calculate total score using the secret list
total = 0 total = eval_score(game, scores_b)
for game_round in game: print(f"total points part b: {total}")
total += eval_score(manipulate_round(game_round)) submit(total, part="b", day=2, year=2022)
print(f'answer part b: total points -> {total}')
submit(total, part='b', day=2, year=2022)