from itertools import permutations from collections import defaultdict def parse(filename): combinations = defaultdict(dict) with open(filename, 'r') as f: for line in f.readlines(): line = line.split() guest1 = line[0] guest2 = line[10][:-1] op = line[2] hap = line[3] if op == 'gain': combinations[guest1][guest2] = hap else: combinations[guest1][guest2] = int(hap) * -1 return combinations def prepare_permutations(): guests = parse('day13.txt').keys() possible_seats = permutations(guests) return possible_seats # print(list(prepare_permutations())) def prepare_couples(): s = [] people = parse('day13.txt') print(people) for perm in prepare_permutations(): print(perm) count = 0 for i in range(len(perm)): # print(people[perm[i]]) # print(people[perm[i][perm[i-1]]]) print(perm[i], perm[i-1], people[perm[i]][perm[i-1]]) count += int(people[perm[i]][perm[i-1]]) s.append(count) # print(s) return max(s) print(prepare_couples())
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question