Update 2015 solutions

This commit is contained in:
2024-10-20 15:19:25 -04:00
parent 87ab42743e
commit e73fa3bae7
16 changed files with 362 additions and 411 deletions

View File

@@ -1,35 +1,32 @@
from lib import get_data
from collections import defaultdict
from itertools import permutations
data = open(0).read().strip()
part_2 = False
data = get_data(__file__).strip()
pairs = defaultdict(int)
people = set()
scores = {}
for line in data.splitlines():
a, _, wl, score, _, _, _, _, _, _, b = line.split()
b = b.replace(".", "")
score = int(score)
if wl == "lose":
score = -score
a, _, gainlose, amount, _, _, _, _, _, _, b = line[:-1].split()
amount = int(amount)
if gainlose == "lose":
amount = -amount
pairs[(a, b)] = amount
people.add(a)
people.add(b)
scores[(a, b)] = score
if part_2:
scores[(a, "me")] = 0
scores[("me", a)] = 0
scores[(b, "me")] = 0
scores[("me", b)] = 0
if part_2:
people.add("me")
max_score = 0
for p in permutations(list(people)):
s = 0
for i in range(len(p)):
a, b = p[i], p[(i + 1) % len(p)]
s += scores[(a, b)]
s += scores[(b, a)]
max_score = max(max_score, s)
def calc_gain(people):
maxgain = 0
for xs in permutations(list(people)):
gain = 0
for i in range(len(xs)):
gain += pairs[(xs[i], xs[(i + 1) % len(xs)])]
gain += pairs[(xs[(i + 1) % len(xs)], xs[i])]
maxgain = max(maxgain, gain)
return maxgain
print(max_score)
print(calc_gain(people))
people.add("Felix")
print(calc_gain(people))