Advent of Code 2022 Day 2(B)
unknown
python
3 years ago
1.6 kB
4
Indexable
scores = { 'rock': 1, 'paper': 2, 'scissors': 3 } results = { 'win': 6, 'draw': 3, 'lose': 0 } action_map = { 'A': 'rock', 'B': 'paper', 'C': 'scissors', 'X': 'lose', 'Y': 'draw', 'Z': 'win' } ring = ['scissors', 'paper', 'rock'] def ring_index(index): ring_size = len(ring) return ((index % ring_size) + ring_size) % ring_size def play(action_index, other): other_index = ring.index(other) win_action = ring_index(other_index - 1) if (action_index == win_action): return 'win' elif action_index == other_index: return 'draw' else: return 'lose' def get_desired_response(opponent_action, player_action): opponent_action_index = ring.index(opponent_action) if player_action == 'win': return ring_index(opponent_action_index - 1) elif player_action == 'lose': return ring_index(opponent_action_index + 1) elif player_action == 'draw': return opponent_action_index games = [] with open('input.txt') as file: for line in file.readlines(): actions = line.removesuffix('\n').split(' ') opponent_action = action_map[actions[0]] player_action = action_map[actions[1]] desired_action_index = get_desired_response(opponent_action, player_action) action_score = scores[ring[desired_action_index]] outcome = play(desired_action_index, opponent_action) games.append(action_score + results[outcome]) print("The sum of the scores for all games is:", sum(games))
Editor is loading...