2015 - Day 13 (Knights of the Dinner Table)

View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3

#Advent of Code
#2015 Day 13, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
from itertools import permutations

def solve(puzzle_input):
    people = []
    units = {}
    for line in puzzle_input:
        line = line.split()
        for person in [line[0], line[-1][:-1]]:
            if person not in people:
                people.append(person)
        if line[2] == "gain":
            units[f"{line[0]},{line[-1][:-1]}"] = int(line[3])
        else:
            units[f"{line[0]},{line[-1][:-1]}"] = -int(line[3])

    def sat(a, b):
        return units[f"{a},{b}"] + units[f"{b},{a}"]

    highest = 0
    for arr in permutations(people):
        running = 0
        temp = list(arr[1:])
        temp.append(arr[0])
        for a, b in zip(arr, temp):
            running += sat(a, b)
        if running > highest:
            highest = running

    return highest

def main():
    puzzle_input = util.read.as_lines()

    highest = solve(puzzle_input)

    print("The total change in happiness for the optimal seating arrangement is " + str(highest) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["Alice would gain 54 happiness units by sitting next to Bob.",
                                       "Alice would lose 79 happiness units by sitting next to Carol.",
                                       "Alice would lose 2 happiness units by sitting next to David.",
                                       "Bob would gain 83 happiness units by sitting next to Alice.",
                                       "Bob would lose 7 happiness units by sitting next to Carol.",
                                       "Bob would lose 63 happiness units by sitting next to David.",
                                       "Carol would lose 62 happiness units by sitting next to Alice.",
                                       "Carol would gain 60 happiness units by sitting next to Bob.",
                                       "Carol would gain 55 happiness units by sitting next to David.",
                                       "David would gain 46 happiness units by sitting next to Alice.",
                                       "David would lose 7 happiness units by sitting next to Bob.",
                                       "David would gain 41 happiness units by sitting next to Carol."]), 330)

if __name__ == "__main__":
    run(main)
Part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3

#Advent of Code
#2015 Day 13, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
from itertools import permutations

def solve(puzzle_input):
    people = []
    units = {}
    for line in puzzle_input:
        line = line.split()
        for person in [line[0], line[-1][:-1]]:
            if person not in people:
                people.append(person)
        if line[2] == "gain":
            units[f"{line[0]},{line[-1][:-1]}"] = int(line[3])
        else:
            units[f"{line[0]},{line[-1][:-1]}"] = -int(line[3])
    for person in people:
        units[f"{person},You"] = 0
        units[f"You,{person}"] = 0
    people.append("You")

    def sat(a, b):
        return units[f"{a},{b}"] + units[f"{b},{a}"]

    highest = 0
    for arr in permutations(people):
        running = 0
        temp = list(arr[1:])
        temp.append(arr[0])
        for a, b in zip(arr, temp):
            running += sat(a, b)
        if running > highest:
            highest = running

    return highest

def main():
    puzzle_input = util.read.as_lines()

    highest = solve(puzzle_input)

    print("The total change in happiness for the optimal seating arrangement is " + str(highest) + ".")

class AOC_Tests(unittest.TestCase):
    @unittest.skip("No test cases")
    def test_ex1(self):
        pass

if __name__ == "__main__":
    run(main)
Return to solution index