Files
adventofcode2022/2/rockpaperscissors.py
2022-12-02 23:54:10 +01:00

101 lines
2.9 KiB
Python

from aocd.models import Puzzle
from aocd import submit
def eval_score(game_round: list) -> int:
'''
calculate game score for single round
'''
score = 0
player_a = game_round[1]
player_b = game_round[0]
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 parse_input(data: list) -> list:
'''
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')]
if __name__ == "__main__":
# get puzzle and parse data
puzzle = Puzzle(year=2022, day=2)
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)
# 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)