forked from varunrau/ResistanceSolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
148 lines (122 loc) · 3.8 KB
/
solver.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
from argparse import ArgumentParser
from constraint import *
import colors
class Round:
def __init__(self, fails, players):
self.num_fails = int(fails)
self.players = players
def __repr__(self):
return "%s %s" % (self.num_fails, self.players)
def constraint(self, players):
pass
def is_relevant(self):
pass
class Mission(Round):
def constraint(self, players):
return sum(players) <= len(players) - self.num_fails
def is_relevant(self):
return self.num_fails > 0
class LadyOfTheLake(Round):
def constraint(self, players):
if len(players) != 2:
return True
p1, p2 = players
if self.num_fails == 1:
return p1 != p2 and p1 != 1
else:
return p1 != 1 or p2 != 0
def is_relevant(self):
return True
class Parser:
def __init__(self, inputfile):
self.rounds = []
self.players = []
self.init_parser(inputfile)
def init_parser(self, inputfile):
lines = inputfile.readlines()
lines = filter(lambda line: line.strip() != "", lines)
lines = [line.strip() for line in lines]
self.parse_nums(lines[:2])
self.parse_players(lines[2])
self.parse_rounds(lines[3:])
def parse_nums(self, arr):
for vals in arr:
val = vals.split()
if val[0].lower() == "spies":
self.spy_count = int(vals[-1])
elif val[0].lower() == "resistance":
self.resistance_count = int(vals[-1])
else:
print "INPUT ERROR"
def parse_players(self, players):
self.players = [Player(p.strip()) for p in players.split(",")]
def parse_rounds(self, rounds):
for r in rounds:
outcome, people = r.split(":")
players = [Player(player.strip()) for player in people.split(",")]
if len(outcome.split()) == 2:
outcome = 0 if outcome.split()[1] == 'pass' else 1
self.rounds.append(LadyOfTheLake(outcome, players))
else:
self.rounds.append(Mission(outcome, players))
@property
def args(self):
return { "resistance" : self.resistance_count,
"spies" : self.spy_count,
"rounds" : self.rounds,
"players" : self.players }
class Player:
def __init__(self, name):
self.name = name
def get_string_val(self, color):
return color + str(self.name) + color
def __repr__(self):
return self.name
def __eq__(self, other):
return self.name == self.name
def __hash__(self):
return hash(self.name)
class Solver:
def __init__(self, args):
self.resistance_count = args["resistance"]
self.spy_count = args["spies"]
self.rounds = args["rounds"]
self.players = args["players"]
possibilities = self.solve()
self.print_possiblities(possibilities)
self.print_statistics(possibilities)
def solve(self):
problem = Problem()
variables = []
for player in self.players:
problem.addVariable(player, [0, 1])
problem.addConstraint(lambda *args: sum(args) == self.resistance_count)
for round in self.rounds:
if round.is_relevant():
problem.addConstraint(lambda *players: round.constraint(players), round.players)
return problem.getSolutions()
def print_possiblities(self, possibilities):
print "*** All Possible Combinations ***"
for p in possibilities:
string = ""
for player in self.players:
if p[player] == 1:
string += player.get_string_val(colors.Color.BLUE)
else:
string += player.get_string_val(colors.Color.RED)
string += " " + colors.Color.END
print string
def print_statistics(self, possibilities):
print "*** Game Statistics ***"
num_possiblities = len(possibilities)
for player in self.players:
print "There are %s out of %s combinations where %s is a resistance" % \
(sum([p[player] for p in possibilities]), num_possiblities, player.name)
if __name__ == '__main__':
parser = ArgumentParser(description="Finds probabilities in Resistance")
parser.add_argument("-i", "--inputfile",
type=file,
required=True,
help="input file")
args = parser.parse_args()
solver = Solver(Parser(args.inputfile).args)