From ef3d1d1081f8de5af8ce2140d786016dd2a7a3b2 Mon Sep 17 00:00:00 2001 From: aaron Date: Fri, 2 Dec 2022 23:54:10 +0100 Subject: [PATCH] solved day 2 --- 2/rockpaperscissors.py | 108 ++++++++++++++++++++++++++++++++--------- README.md | 6 +++ 2 files changed, 90 insertions(+), 24 deletions(-) diff --git a/2/rockpaperscissors.py b/2/rockpaperscissors.py index 51c92e2..bab97fc 100644 --- a/2/rockpaperscissors.py +++ b/2/rockpaperscissors.py @@ -1,40 +1,100 @@ from aocd.models import Puzzle from aocd import submit -class Game: +def eval_score(game_round: list) -> int: + ''' + calculate game score for single round + ''' - # data fields - moves = list() - opponent = list() + 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 __init__(self, moves : list, opponent : list) -> None: - self.moves = moves.copy() - self.opponent = opponent.copy() def parse_input(data: list) -> list: ''' parses the input data and generates a list of elfs ''' - # split input string into move sets - splits = data.split('\n') - # split move set list into game sets - n = 3 - games = [ splits[i:i+n] for i in range(0, len(splits), n) ] - print(games) + # 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) - data = puzzle.input_data - parse_input(data) - print(len(data)) + 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 a: submit biggest number of calories - answer = 0 - print(f'answer part a: {answer}') - #submit(answer, part='a', day=2, year=2022) - - # part b: submit the calorie sum of the top 3 elfs - #print(f'answer part b: {answer}') - #submit(answer, part='b', 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) diff --git a/README.md b/README.md index a02dde3..fea91ae 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,9 @@ * [python aocd library](https://pypi.org/project/advent-of-code-data/) +## session token + +```bash +$ cat .env +export AOC_SESSION="53616c..." +```