Day 13 - Advent of Code 2015

Working solutions for the day 13 puzzles.

Parts One and Two

""" day_13_01_02.py """

# usage: python3 day_13_01_02.py < input

import itertools
import sys


def happiness_potential(group, reference):
    """ calculate happiness potential """
    size = len(group)
    total = 0
    for i, person in enumerate(group):
        total += reference.get((person, group[(i + 1) % size]), 0)
        total += reference.get((person, group[(i - 1) % size]), 0)
    return total


guests = set()
happiness = {}
for line in sys.stdin.read().splitlines():
    words = line.split()
    guest, neighbour = words[0], words[-1][:-1]
    guests.add(guest)
    value = int(words[3])
    if words[2] == 'lose':
        value = -value
    happiness[(guest, neighbour)] = value

# part 01
potential = {happiness_potential(sitting, happiness)
             for sitting in itertools.permutations(guests)}
print(max(potential))

# part 02
guests.add('self')
potential = {happiness_potential(sitting, happiness)
             for sitting in itertools.permutations(guests)}
print(max(potential))