Day 1

#!/usr/bin/env python

input = open("puzzle_input").read().strip()

def part1(input):
    elfs = input.split("\n\n")
    
    elfs_int = []
    for elf in elfs:
        e = elf.split("\n")
        e = map(int, e)
        e = sum(e)
        elfs_int.append(e)
    
    return max(elfs_int)
    

def part2(input):
    elfs = input.split("\n\n")
    
    elfs_int = []
    for elf in elfs:
        e = elf.split("\n")
        e = map(int, e)
        e = sum(e)
        elfs_int.append(e)
    
    elfs_int = sorted(elfs_int, reverse=True)
    
    return sum(elfs_int[:3])
        
print(part2(input))

Day 2

#!/usr/bin/env python
from enum import Enum

input = open("puzzle_input").read().strip()

class Opponent(str, Enum):
    ROCK = "A",
    PAPER = "B",
    SCISSORS = "C"

class Me(str, Enum):
    ROCK = "X",
    PAPER = "Y",
    SCISSORS= "Z"

class Condition(str, Enum):
    LOOSE = "X",
    DRAW = "Y",
    WIN = "Z"
    
shape_values = {
    "A": 1,
    "B": 2,
    "C": 3,
    "X": 1,
    "Y": 2,
    "Z": 3
}

def determine_winner(opponent, me):
    opponent = Opponent(opponent)
    me = Me(me)
    if opponent == Opponent.ROCK:
        if me == Me.ROCK:
            return None
        elif me == Me.PAPER:
            return "me"
        elif me == Me.SCISSORS:
            return "opponent"
        
    elif opponent == Opponent.PAPER:
        if me == Me.ROCK:
            return "opponent"
        elif me == Me.PAPER:
            return None
        elif me == Me.SCISSORS:
            return "me"
    
    elif opponent == Opponent.SCISSORS:
        if me == Me.ROCK:
            return "me"
        elif me == Me.PAPER:
            return "opponent"
        elif me == Me.SCISSORS:
            return None
    
    else:
        print("Error, not a valid move:", opponent, me)            

def score_line(opponent, me):
    my_score = 0
    my_bonus = shape_values[me]
    opponent_bonus = shape_values[opponent]
    
    winner = determine_winner(opponent, me)
    
    if winner == "me":
        my_score = 6
    elif winner == None:
        my_score = 3
    elif winner == "opponent":
        my_score = 0
    
    my_score += my_bonus
    return my_score

def select_move(opponent, cond):
    if opponent == Opponent.ROCK:
        if cond == Condition.LOOSE:
            return Me.SCISSORS
        elif cond == Condition.DRAW:
            return Me.ROCK
        elif cond == Condition.WIN:
            return Me.PAPER
    elif opponent == Opponent.PAPER:
        if cond == Condition.LOOSE:
            return Me.ROCK
        elif cond == Condition.DRAW:
            return Me.PAPER
        elif cond == Condition.WIN:
            return Me.SCISSORS
    elif opponent == Opponent.SCISSORS:
        if cond == Condition.LOOSE:
            return Me.PAPER
        elif cond == Condition.DRAW:
            return Me.SCISSORS
        elif cond == Condition.WIN:
            return Me.ROCK
                    
def part1(input):
    lines = input.split("\n")
    
    total_score = 0
    for line in lines:
        e = line.split(" ")
        total_score += score_line(e[0], e[1])
    
    return total_score

def part2(input):
    lines = input.split("\n")
    
    total_score = 0
    for line in lines:
        e = line.split(" ")
        total_score += score_line(e[0], select_move(e[0], e[1]))
    
    return total_score

print(part1(input))
print(part2(input))

Day 3

Coming…