-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanagrama_stack.rb~
76 lines (57 loc) · 1.48 KB
/
anagrama_stack.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
require 'parallel'
require 'time'
require 'benchmark'
class Bot
def initialize(nombre,diccL,intentos,termine,consola)
@nombre,@diccL,@intentos,@termine,@consola=nombre,diccL,intentos,termine,consola
end
def buscarAlt()
for intento in @intentos
for palabra in @diccL
if palabra[1]==intento
@consola.synchronize{puts "\n============Anagrama-> " + @nombre+":"+palabra[1]}
palabra = nil
end
intento = nil
end
end
end
end
class Master
def initialize(base)
#number of bots(CPU cores)
@K=4
#Base is the keyword to find the anagrama
@base=base
#Loading Dict
fDicc="alternativo"
dicc=File.open(fDicc)
@diccL=Hash.new
i=0
dicc.each do |linea|
@diccL[i]=linea.to_s.chomp
i+=1
end
dicc.close
@consola=Mutex.new
@colaBots=Queue.new
@[email protected]("").permutation.map{|p| p.join("")}
end
def iniciar()
time = Benchmark.realtime do
inicio=Time.new
puts "iniciar"
fin=@K-1
b=[]
for i1 in 0..(fin)
i=i1.to_i;
b[i]= Bot.new("Bot" + i.to_s,@diccL,@permutaciones[i*(@permutaciones.length/@K)..(i+1)*(@permutaciones.length/@K)],@colaBots,@consola)
end
Parallel.map(b) do |bots|
bots.buscarAlt()
end
@consola.synchronize{puts "termino";fin =Time.new; puts fin.to_i - inicio.to_i }
end
puts (time*1000).to_s
end
end