-
Notifications
You must be signed in to change notification settings - Fork 1
/
actor_picker_versus.py
216 lines (180 loc) · 7.08 KB
/
actor_picker_versus.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import sys
class Gender:
Male = 0
Female = 1
class Graph(object):
__slots__ = ['actresses', 'actors', 'n', 'm', 'role']
def __init__(self) -> None:
super().__init__()
self.actresses = set()
self.actors = set()
self.n = 0
self.m = 0
self.role = None
def parse_input(self) -> None:
n, m = input().rstrip().split()
self.n = int(n)
self.m = int(m)
self.actresses = {}
for i in range(self.n):
name = input().rstrip()
self.actresses[name] = Actress(name, i)
self.actors = {}
for i in range(self.n):
name = input().rstrip()
self.actors[name] = Actor(name, i)
people_dict = {**self.actresses, **self.actors}
for _ in range(self.m):
movie = Movie(input().rstrip())
for _ in range(int(input().rstrip())):
movie.add_cast(people_dict[input().rstrip()])
for person in movie.female_cast:
person.costarred |= movie.male_cast
for person in movie.male_cast:
person.costarred |= movie.female_cast
self.role = input().rstrip()
if self.role == "Veronique":
self.play_veronique()
else:
self.play_mark()
def play_veronique(self):
actor_name = None
while True:
# Find out whether we are in a winning position
winning = not self.match_all()
actress = None
if winning:
# If we are in a winning situation, then
# - if this is the first pick,
# get list of unmatched actresses
# - otherwise
# get list of unmatched actresses who are
# adjacent to the previously picked actor
if actor_name is None:
options = [actress for actress in self.actresses.values() if actress.match is None]
else:
options = [actress for actress in set(self.actresses.values()) & self.actors[actor_name].costarred if actress.match is None]
# Pick the first actress naively
if options:
actress = options[0]
else:
# If we are not in a winning situation, then
# - if this is the first pick,
# get list of all actresses
# - otherwise
# get list of all actresses who are
# adjacent to the previously picked actor.
if actor_name is None:
options = list(self.actresses.values())
else:
options = list(actress for actress in set(self.actresses.values()) & self.actors[actor_name].costarred)
# Pick the first actress naively
if options:
actress = options[0]
# Reset actresses and actors for next iteration of checking whether
# we are winning or not.
self.reset()
# If an actress is picked, remove it from being picked again
# and output it
# Otherwise output "IGiveUp"
if actress:
del self.actresses[actress.name]
print(actress.name)
else:
print("IGiveUp")
# Handle input
actor_name = input().rstrip()
actor = self.actors[actor_name]
# Prevent this actor from being explored again
actor.explored = self.n + 1
def play_mark(self):
while True:
# Handle input
actress_name = input().rstrip()
actress = self.actresses[actress_name]
# Find out whether we are in a winning position
winning = self.match_all()
# Remove the actress from being used again
del self.actresses[actress_name]
actor = None
if winning:
# If we are in a winning situation,
# pick the match of the aforementioned actress.
actor = actress.match
else:
# If we are not in a winning situation,
# just naively pick an unpicked, adjacent actor.
options = [actor for actor in actress.costarred if actor.explored < self.n]
if options:
actor = options[0]
# Reset actresses and actors for next iteration of checking whether
# we are winning or not.
self.reset()
# If an actor is picked, remove it from being picked again
# and output it
# Otherwise output "IGiveUp"
if actor:
actor.explored = self.n + 1
print(actor.name)
else:
print("IGiveUp")
def reset(self):
# Reset everything for next call
# of self.match_all()
for actor in self.actors.values():
if actor.explored < self.n:
actor.explored = -1
actor.match = None
for actress in self.actresses.values():
actress.match = None
def match(self, actress, i):
for func in (lambda act: not act.match, lambda act: act.match):
for actor in actress.costarred:
if func(actor) and actor.explored < i:
actor.explored = i
if actor.match is None or self.match(actor.match, i):
actor.match = actress
actress.match = actor
return True
return False
def match_all(self):
pbm = True
for i, actress in enumerate(sorted(self.actresses.values(), key=lambda a: len(a.costarred))):
if not self.match(actress, i):
pbm = False
return pbm
class Movie(object):
__slots__ = ['name', 'male_cast', 'female_cast']
def __init__(self, name: str) -> None:
super().__init__()
self.name = name
self.male_cast = set()
self.female_cast = set()
def add_cast(self, person: "Person") -> None:
if person.gender:
self.female_cast.add(person)
else:
self.male_cast.add(person)
class Person(object):
__slots__ = ['name', 'id', 'gender', 'match', 'costarred']
def __init__(self, name: str, _id: int, gender: "Gender"):
super().__init__()
self.name = name
self.id = _id
self.gender = gender
self.match = None
self.costarred = set()
def __hash__(self) -> int:
return self.id
class Actress(Person):
__slots__ = []
def __init__(self, name: str, _id: int) -> None:
super().__init__(name, _id, Gender.Female)
class Actor(Person):
__slots__ = ['explored']
def __init__(self, name: str, _id: int) -> None:
super().__init__(name, _id, Gender.Male)
self.explored = -1
if __name__ == "__main__":
g = Graph()
g.parse_input()