-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathayto.rb
248 lines (169 loc) · 6.08 KB
/
ayto.rb
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
217
218
219
220
221
class Integer
def factorial
(1..self).reduce(:*) || 1
end
end
class Ceremony
attr_accessor :pairs
attr_accessor :lights
attr_accessor :episode
def initialize(pairs, lights, episode)
@pairs = pairs
@lights = lights
@episode = episode
end
end
class Truth
attr_accessor :guy
attr_accessor :girl
attr_accessor :truth
attr_accessor :episode
def initialize(guy, girl, truth, episode)
@guy = guy
@girl = girl
@truth = truth
@episode = episode
end
end
class Ayto
def initialize(arg = {})
raise ArgumentError, "Invalid arguments to initialize AYTO. Need 2 arrays of 10 unique strings each." unless
arg[:guys].kind_of?(Array) &&
arg[:girls].kind_of?(Array) &&
arg[:guys].length == arg[:girls].length &&
arg[:guys].uniq.length == arg[:guys].length &&
arg[:girls].uniq.length == arg[:girls].length &&
(arg[:guys]+arg[:girls]).uniq.length == (arg[:guys]+arg[:girls]).length
@cardinality = arg[:guys].length
@guys = arg[:guys]
@girls = arg[:girls]
@truths = []
@ceremonies = []
end
def addTruth(arg = {})
raise ArgumentError, "Invalid arguments passed to addTruth." unless
@guys.include?(arg[:guy]) &&
@girls.include?(arg[:girl]) &&
(arg[:truth] == :NO_MATCH || arg[:truth] == :PERFECT_MATCH) &&
arg[:episode].is_a?(Numeric)
@truths.push(Truth.new(arg[:guy], arg[:girl], arg[:truth], arg[:episode]))
end
def addCeremony(arg = {})
raise ArgumentError, "Invalid arguments passed to addCeremony. (E1)" unless
arg[:pairs].kind_of?(Array)
raise ArgumentError, "Invalid arguments passed to addCeremony. (E2)" unless
arg[:pairs].count == @cardinality
raise ArgumentError, "Invalid arguments passed to addCeremony. (E3)" unless
arg[:lights].is_a?(Integer)
raise ArgumentError, "Invalid arguments passed to addCeremony. (E4)" unless
arg[:lights].between?(0,@cardinality)
raise ArgumentError, "Invalid arguments passed to addCeremony. (E5)" unless
arg[:episode].is_a?(Numeric)
raise ArgumentError, "Invalid arguments passed to addCeremony. (E6)" unless
(arg[:pairs].map{|x| x[:guy]} & @guys).count == @cardinality
raise ArgumentError, "Invalid arguments passed to addCeremony. (E7)" unless
(arg[:pairs].map{|x| x[:girl]} & @girls).count == @cardinality
@ceremonies.push(Ceremony.new(arg[:pairs], arg[:lights], arg[:episode]))
end
def getResults(arg = {})
raise ArgumentError, "Invalid arguments passed to getResults: :episode must be numeric" unless
!arg.key?(:episode) || arg[:episode].is_a?(Numeric)
if !arg.key?(:episode)
arg[:episode] = Float::INFINITY
end
total = 0
counts = Array.new(@cardinality * @cardinality).fill(0)
matches = @truths.select{|x| x.truth == :PERFECT_MATCH && x.episode <= arg[:episode]}.map{|x| [@guys.index(x.guy), @girls.index(x.girl)]}
leftMatches = matches.map{|x| x[0]}
rightMatches = matches.map{|x| x[1]}
matchesLength = matches.length
nonmatches = @truths.select{|x| x.truth == :NO_MATCH && x.episode <= arg[:episode]}.map{|x| [@guys.index(x.guy), @girls.index(x.girl)]}
leftNonmatches = nonmatches.map{|x| x[0]}
rightNonmatches = nonmatches.map{|x| x[1]}
nonmatchesLength = nonmatches.length
lights = @ceremonies.select{|x| x.episode <= arg[:episode]}.map{|x| x.lights}
ceremonies = @ceremonies.select{|c| c.episode <= arg[:episode]}.map{|c| c.pairs.sort_by{|s| @guys.index(s[:guy])}.map{|p| @girls.index(p[:girl])}}
ceremonies = ceremonies.reduce(:+)
ceremoniesLength = lights.length
permuted = Array.new(@cardinality)
elements = Array.new(@cardinality)
identityPermutation = (0...@cardinality).to_a
identityPermutation.permutation.each do |permuted|
valid = true
# Verify the matches
(0...matchesLength).each do |i|
if permuted[leftMatches[i]] != rightMatches[i]
valid = false
break
end
end
next if !valid
# Verify the nonmatches
(0...nonmatchesLength).each do |i|
if permuted[leftNonmatches[i]] == rightNonmatches[i]
valid = false
break
end
end
next if !valid
# Verify the matching ceremonies
(0...ceremoniesLength).each do |i|
correct = 0
(0...@cardinality).each do |j|
if permuted[j] == ceremonies[i * @cardinality + j]
correct += 1
end
end
if correct != lights[i]
valid = false
break
end
end
next if !valid
# Record the result
total += 1
(0...@cardinality).each do |i|
counts[i * @cardinality + permuted[i]] += 1
end
end
results = Array.new(@cardinality){Array.new(@cardinality)}
(0...@cardinality).each do |x|
(0...@cardinality).each do |y|
results[x][y] = counts[x * @cardinality + y]
end
end
return {:total => total, :results => results}
end
def printResults(arg = {})
results = getResults(arg)
puts "#{results[:total].to_s.reverse.gsub(/...(?=.)/,'\&,').reverse} possibilities remain.\n\n"
w = (@guys+@girls).max_by do |e|
e.length
end
w = w.length
w = 6 if w < 6
header = ' ' * w
(0...@cardinality).each do |x|
header << '|' + sprintf("%#{w}s", @guys[x])
end
header << "\n" + ('-' * w)
(0...@cardinality).each do |x|
header << '+' + sprintf("%#{w}s", ('-' * w))
end
header << "\n"
(0...@cardinality).each do |y|
header << sprintf("%#{w}s", @girls[y])
(0...@cardinality).each do |x|
header << '|' + sprintf("%#{w}.1f", (100.0 * results[:results][x][y].to_f)/results[:total])
end
header << "\n" + ('-' * w)
(0...@cardinality).each do |x|
header << '+' + sprintf("%#{w}s", ('-' * w))
end
header << "\n"
end
puts header
end
end
require './season5.rb'
puts S5.printResults()