|
15 | 15 | # - одновременно можно запускать не более одного
|
16 | 16 | #
|
17 | 17 | def a(value)
|
18 |
| - puts "https://localhost:9292/a?value=#{value}" |
19 |
| - Faraday.get("https://localhost:9292/a?value=#{value}").body |
| 18 | + Thread.new do |
| 19 | + puts "https://localhost:9292/a?value=#{value}" |
| 20 | + Faraday.get("https://localhost:9292/a?value=#{value}").body |
| 21 | + end |
20 | 22 | end
|
21 | 23 |
|
22 | 24 | def b(value)
|
23 |
| - puts "https://localhost:9292/b?value=#{value}" |
24 |
| - Faraday.get("https://localhost:9292/b?value=#{value}").body |
| 25 | + Thread.new do |
| 26 | + puts "https://localhost:9292/b?value=#{value}" |
| 27 | + Faraday.get("https://localhost:9292/b?value=#{value}").body |
| 28 | + end |
25 | 29 | end
|
26 | 30 |
|
27 | 31 | def c(value)
|
28 |
| - puts "https://localhost:9292/c?value=#{value}" |
29 |
| - Faraday.get("https://localhost:9292/c?value=#{value}").body |
| 32 | + Thread.new do |
| 33 | + puts "https://localhost:9292/c?value=#{value}" |
| 34 | + Faraday.get("https://localhost:9292/c?value=#{value}").body |
| 35 | + end |
30 | 36 | end
|
31 | 37 |
|
32 | 38 | # Референсное решение, приведённое ниже работает правильно, занимает ~19.5 секунд
|
33 | 39 | # Надо сделать в пределах 7 секунд
|
34 | 40 |
|
35 | 41 | def collect_sorted(arr)
|
36 |
| - arr.sort.join('-') |
| 42 | + arr.map(&:value).sort.join('-') |
37 | 43 | end
|
38 | 44 |
|
39 | 45 | start = Time.now
|
40 | 46 |
|
| 47 | +### 1 queue |
41 | 48 | a11 = a(11)
|
42 | 49 | a12 = a(12)
|
43 | 50 | a13 = a(13)
|
44 | 51 | b1 = b(1)
|
| 52 | +b2 = b(2) |
45 | 53 |
|
46 |
| -ab1 = "#{collect_sorted([a11, a12, a13])}-#{b1}" |
| 54 | +ab1 = "#{collect_sorted([a11, a12, a13])}-#{b1.value}" |
47 | 55 | puts "AB1 = #{ab1}"
|
48 | 56 |
|
49 | 57 | c1 = c(ab1)
|
50 | 58 | puts "C1 = #{c1}"
|
51 | 59 |
|
| 60 | +### 2 queue |
52 | 61 | a21 = a(21)
|
53 | 62 | a22 = a(22)
|
54 | 63 | a23 = a(23)
|
55 |
| -b2 = b(2) |
| 64 | +b3 = b(3) |
56 | 65 |
|
57 |
| -ab2 = "#{collect_sorted([a21, a22, a23])}-#{b2}" |
| 66 | +ab2 = "#{collect_sorted([a21, a22, a23])}-#{b2.value}" |
58 | 67 | puts "AB2 = #{ab2}"
|
59 | 68 |
|
60 | 69 | c2 = c(ab2)
|
61 | 70 | puts "C2 = #{c2}"
|
62 | 71 |
|
| 72 | +### 3 queue |
63 | 73 | a31 = a(31)
|
64 | 74 | a32 = a(32)
|
65 | 75 | a33 = a(33)
|
66 |
| -b3 = b(3) |
67 | 76 |
|
68 |
| -ab3 = "#{collect_sorted([a31, a32, a33])}-#{b3}" |
| 77 | +ab3 = "#{collect_sorted([a31, a32, a33])}-#{b3.value}" |
69 | 78 | puts "AB3 = #{ab3}"
|
70 | 79 |
|
71 | 80 | c3 = c(ab3)
|
72 | 81 | puts "C3 = #{c3}"
|
73 | 82 |
|
74 | 83 | c123 = collect_sorted([c1, c2, c3])
|
75 |
| -result = a(c123) |
| 84 | +result = a(c123).value |
76 | 85 |
|
77 | 86 | puts "FINISHED in #{Time.now - start}s."
|
78 | 87 | puts "RESULT = #{result}" # 0bbe9ecf251ef4131dd43e1600742cfb
|
0 commit comments