solved day 2

This commit is contained in:
aaron
2022-12-02 23:54:10 +01:00
parent 86e0f0a90e
commit ef3d1d1081
2 changed files with 90 additions and 24 deletions

View File

@@ -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)

View File

@@ -4,3 +4,9 @@
* [python aocd library](https://pypi.org/project/advent-of-code-data/)
## session token
```bash
$ cat .env
export AOC_SESSION="53616c..."
```