Skip to content

Instantly share code, notes, and snippets.

@rogerallen
Last active December 3, 2022 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerallen/a25797f88e30d180a17619f58f7b88db to your computer and use it in GitHub Desktop.
Save rogerallen/a25797f88e30d180a17619f58f7b88db to your computer and use it in GitHub Desktop.
AOC 22

AOC 22

Sharing my code for AOC

# python3 aoc22_day1.py
# by Roger Allen
from itertools import groupby
# Day 1
with open('aoc22_input01.txt','r') as f:
in1 = f.read()
# how many calories does the elf with the most calories carry?
elf_calories = [list(g) for k,g in groupby(in1.split('\n'),lambda x: x!='') if k == True]
elf_calories = [[int(x) for x in g] for g in elf_calories]
sum_elf_calories = [sum(g) for g in elf_calories]
max_elf_calories = max(sum_elf_calories)
print(max_elf_calories)
sum_elf_calories.sort()
# how many calories do the top 3 elves carry?
print(sum(sum_elf_calories[-3:]))
# python3 aoc22_day2.py
with open('aoc22_input02.txt','r') as f:
encrypted_strategy_guide = f.read()
# ----------------------------------------------------------------------
# Part 1
decoder_m = {
'A': 'rock',
'X': 'rock',
'B': 'paper',
'Y': 'paper',
'C': 'scissors',
'Z': 'scissors'
}
shape_score_m = {
'rock': 1, 'paper': 2, 'scissors': 3
}
game_score_m = {
('rock','rock'): 3,
('rock','paper'): 6,
('rock','scissors'): 0,
('paper','rock'): 0,
('paper','paper'): 3,
('paper','scissors'): 6,
('scissors','rock'): 6,
('scissors','paper'): 0,
('scissors','scissors'): 3
}
class Game:
def __init__(self,oppo_choice,your_choice):
self.oppo_choice = decoder_m[oppo_choice]
self.your_choice = decoder_m[your_choice]
self.score = shape_score_m[self.your_choice] + game_score_m[(self.oppo_choice, self.your_choice)]
# Testing
print(
Game('A','Y').score == 8,
Game('B','X').score == 1,
Game('C','Z').score == 6,
Game('A','Y').score + Game('B','X').score + Game('C','Z').score == 15)
# What is total score?
game_scores = [Game(*x.split()).score for x in encrypted_strategy_guide.split('\n')]
total_score = sum(game_scores)
print(total_score)
# ----------------------------------------------------------------------
# Part 2
# X means you need to lose, Y means you need to end the round in a draw, and Z means you need to win.
decoder2_m = {
'A': 'rock',
'X': 'lose',
'B': 'paper',
'Y': 'draw',
'C': 'scissors',
'Z': 'win'
}
shape_score2_m = {
'rock': 1, 'paper': 2, 'scissors': 3
}
game_score2_m = {
('rock','lose'): 0 + shape_score2_m['scissors'],
('rock','draw'): 3 + shape_score2_m['rock'],
('rock','win'): 6 + shape_score2_m['paper'],
('paper','lose'): 0 + shape_score2_m['rock'],
('paper','draw'): 3 + shape_score2_m['paper'],
('paper','win'): 6 + shape_score2_m['scissors'],
('scissors','lose'): 0 + shape_score2_m['paper'],
('scissors','draw'): 3 + shape_score2_m['scissors'],
('scissors','win'): 6 + shape_score2_m['rock']
}
class Game2:
def __init__(self,oppo_choice,your_choice):
self.oppo_choice = decoder2_m[oppo_choice]
self.your_choice = decoder2_m[your_choice]
self.score = game_score2_m[(self.oppo_choice, self.your_choice)]
# Testing
print(
Game2('A','Y').score == 4,
Game2('B','X').score == 1,
Game2('C','Z').score == 7,
Game2('A','Y').score + Game2('B','X').score + Game2('C','Z').score == 12)
# what is total score?
game_scores = [Game2(*x.split()).score for x in encrypted_strategy_guide.split('\n')]
total_score = sum(game_scores)
print(total_score)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment