-
Notifications
You must be signed in to change notification settings - Fork 0
/
christmas-in-a-script.py
executable file
·44 lines (37 loc) · 1.73 KB
/
christmas-in-a-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
#!/usr/bin/env python
import random, sys
families = [['Kyle', 'Jody'], ['Roberta', 'Jeff'], ['Tim', 'Heidi'], ['Owen'], ['Hillary', 'Jamie']]
individuals = [item for sublist in families for item in sublist]
def in_family(individual1, individual2):
for family in families:
if individual1 in family and individual2 in family:
return True
return False
max_iterations = 2000
count = len(individuals)
allocated = []
allocations = {}
failed = False
for giver_id, giver in enumerate(individuals):
recipient_id = random.randint(0, count - 1) #sometimes you just gotta trust the PRNG
iterations = 0
#can't give to self, can't give to already allocated recipient, can't give to somebody giving to you
while (recipient_id == giver_id) or (recipient_id in allocated) or (recipient_id in allocations and allocations[recipient_id] == giver_id):
iterations += 1
recipient_id = random.randint(0, count - 1)
#can't give to somebody in your family
if in_family(individuals[recipient_id], individuals[giver_id]):
recipient_id = giver_id
if iterations >= max_iterations: #lazy avoidance of bad allocation scenarios
print('Try again, hit max number of attempts ({0})'.format(max_iterations))
print('\tUnallocated: {0}'.format([individual for individual_id, individual in enumerate(individuals) if individual_id not in allocated]))
failed = True
break
if failed:
break
allocated.append(recipient_id)
allocations[giver_id] = recipient_id
for giver_id in allocations:
giver_name = individuals[giver_id]
recipient_name = individuals[allocations[giver_id]]
print("{0} -> {1}".format(giver_name, recipient_name))