-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.rb
213 lines (194 loc) · 6.15 KB
/
router.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
class Router
require "./steps"
include Steps
HORIZONTAL = [:up_mesh, :h_cycle, :down_mesh]
VERTICAL = [:left_mesh, :v_cycle, :right_mesh]
K = { 1 => :up_mesh, 2 => :h_cycle, 3 => :down_mesh}
L = { 1 => :left_mesh, 2 => :v_cycle, 3 => :right_mesh}
attr_reader :whole_path
def initialize(step)
@step = step
@broken_nodes = []
@possible_variants = {
:forward_way => true,
:tried => []
}
end
def move_horizontal(to_j=nil)
@possible_variants[:forward_way] = true
@possible_variants[:tried] = []
@possible_variants[:i_tried] << @from[:i]
to_j ||= @to[:j]
@current_state[:orientation] = :h
@current_state[:forward] = forward_way?(@from[:j], to_j)
while @from[:j] != to_j do
if in_claster?
test = @from.dup
test[:l] = @current_state[:forward] ? 3 : 1
ok = build_in_claster(test)
while !ok do
@possible_variants[:tried] << @current_state[:type_h]
if @possible_variants[:tried].size == 3
@possible_variants[:tried] = []
return false unless @possible_variants[:forward_way]
@possible_variants[:forward_way] = false
@current_state[:forward] = !@current_state[:forward]
@current_state[:type_h] = K[@from[:k]]
break
end
@current_state[:type_h] = (HORIZONTAL - @possible_variants[:tried]).first
test = @from.dup
test[:l] = @current_state[:forward] ? 3 : 1
test[:k] = K.key(@current_state[:type_h])
ok = build_in_claster(test)
end
else
unless next_step
puts "FAILED HORIZONTAL"
return false
end
end
p @from
gets
end
true
end
def move_vertical(to_i=nil)
@possible_variants[:forward_way] = true
@possible_variants[:tried] = []
@possible_variants[:j_tried] << @from[:j]
to_i ||= @to[:i]
@current_state[:orientation] = :v
@current_state[:forward] = forward_way?(@from[:i], to_i)
while @from[:i] != to_i do
if in_claster?
test = @from.dup
test[:k] = @current_state[:forward] ? 3 : 1
ok = build_in_claster(test)
while !ok do
@possible_variants[:tried] << @current_state[:type_v]
if @possible_variants[:tried].size == 3
@possible_variants[:tried] = []
return false unless @possible_variants[:forward_way]
@possible_variants[:forward_way] = false
@current_state[:forward] = !@current_state[:forward]
@current_state[:type_v] = L[@from[:l]]
break
end
@current_state[:type_v] = (VERTICAL - @possible_variants[:tried]).first
test = @from.dup
test[:k] = @current_state[:forward] ? 3 : 1
test[:l] = L.key(@current_state[:type_v])
ok = build_in_claster(test)
end
else
unless next_step
puts "FAILED VERTICAL"
return false
end
end
p @from
gets
end
true
end
def one_to_one(from, to)
@possible_variants = {
:forward_way => true,
:tried => [],
:i_tried => [],
:j_tried => []
}
@whole_path = [from.map(&:last).join]
@from = from
@to = to
@current_state = start_state(from, to)
p @current_state
moved_horizontal = @from[:j] == @to[:j]
ok = true
while !moved_horizontal
puts "moving horizontal"
p @possible_variants
moved_horizontal = move_horizontal if ok
break if move_horizontal
to_i = ((1..@step).to_a - @possible_variants[:i_tried]).first
ok = move_vertical(to_i)
@possible_variants[:i_tried] << to_i unless ok
if @possible_variants[:i_tried].size == @step
puts "FAILED TO FIND PATH ANY HORIZONTAL"
return
end
end
puts "Horizontal moved"
moved_vertical = @from[:i] == @to[:i]
ok = true
while !moved_vertical
puts "moving vertical"
p @possible_variants
moved_vertical = move_vertical if ok
break if moved_vertical
to_j = ((1..@step).to_a - @possible_variants[:j_tried]).first
ok = move_horizontal(to_j)
@possible_variants[:j_tried] << to_j unless ok
if @possible_variants[:j_tried].size == @step
puts "FAILED TO FIND PATH ANY VERTICAL"
return
end
end
puts "Vertical moved"
if @from[:j] != @to[:j]
puts "Transfered on right row, try to go to the right column"
unless move_horizontal
puts "FAILED TO FIND PATH HORIZONTAL, sorry"
return
end
end
puts "before claster search"
p @from
p @to
puts "FAILED TO FIND PATH in claster" unless build_in_claster(@to)
end
def next_step
self.send(@current_state["type_#{@current_state[:orientation]}".to_sym])
end
def start_state(from, to)
{
:forward => forward_way?(from[:i],to[:i]),
:orientation => :v,
:type_h => K[from[:k]],
:type_v => L[from[:l]]
}
end
def refresh_current_state
@current_state[:type_h] = K[@from[:k]]
@current_state[:type_v] = L[@from[:l]]
end
def in_claster?
!((@current_state[:orientation] == :v && @current_state[:forward] && @from[:k] == 3)||
(@current_state[:orientation] == :v && !@current_state[:forward] && @from[:k] == 1)||
(@current_state[:orientation] == :h && @current_state[:forward] && @from[:l] == 3)||
(@current_state[:orientation] == :h && !@current_state[:forward] && @from[:l] == 1))
end
def forward_way?(from, to)
reverse = false
from, to, reverse = to, from, true if from > to
((to - from) < (from + @step - to)) ^ reverse
end
def break!(to_break)
@broken_nodes << to_break
p @broken_nodes
end
def broken?(node)
puts "!! Tryed to go to --> #{node.map(&:last).join} but it's BROKEN!" if @broken_nodes.include?(node.map(&:last).join)
@whole_path << "try to broken #{node.map(&:last).join}" if @broken_nodes.include?(node.map(&:last).join)
@broken_nodes.include?(node.map(&:last).join)
end
def heal!(node)
@broken_nodes -= [node]
p @broken_nodes
end
def put_node(node)
@whole_path << node.map(&:last).join
puts "go to --> #{node.map{|k,v| v}.join}"
end
end