-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
59 lines (48 loc) · 1.76 KB
/
script.py
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
import sys
import itertools
from pprint import pprint
def build_tables(relationships):
people = list(relationships.keys())
return [[people[0]] + list(l) for l in itertools.permutations(people[1:])]
def score(table, relationships):
score = 0
for pos, person in enumerate(table):
score += relationships[person][table[pos-1]]
if pos+1 < len(table):
score += relationships[person][table[pos+1]]
else:
score += relationships[person][table[0]]
return score
def highest_score(tables, relationships):
high_score = 0
high_table = []
for table in tables:
table_score = score(table, relationships)
if table_score > high_score:
high_score = table_score
high_table = table
return high_score, high_table
if __name__ == '__main__':
inputs = (line.rstrip('\n') for line in open(sys.argv[1]))
relationships = {}
for line in inputs:
person, next_to = line.split(' happiness units by sitting next to ')
next_to = next_to[:-1]
person, happy = person.split(' would ')
change, happy = happy.split()
happy = int(happy) if change == 'gain' else -int(happy)
if person not in relationships:
relationships[person] = {}
relationships[person][next_to] = happy
# p1
tables = build_tables(relationships)
high_score, high_table = highest_score(tables, relationships)
print(high_score, high_table)
# p2
relationships['Ghosty'] = {}
for person in relationships.keys():
relationships['Ghosty'][person] = 0
relationships[person]['Ghosty'] = 0
tables = build_tables(relationships)
high_score, high_table = highest_score(tables, relationships)
print(high_score, high_table)