-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathgarage.rb
More file actions
55 lines (49 loc) · 1 KB
/
garage.rb
File metadata and controls
55 lines (49 loc) · 1 KB
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
class FastList
def initialize(arr)
@arr = arr
@indexof = Hash[arr.map.with_index { |e, i| [e, i] }]
end
def swap(el1, el2)
i1 = @indexof[el1]
i2 = @indexof[el2]
# set element where 0 is to final element
@arr[i1] = el2
# update dict
@indexof[el2] = i1
# set 0 where the previous number was
@arr[i2] = el1
# update dict
@indexof[el1] = i2
@moves += 1
end
def calc_moves(final)
@moves = 0
until @arr == final
i0 = @indexof[0]
if final[i0] != 0 # if element can be moved to its final position
swap(0, final[i0])
p @arr
next
end
@arr.each.with_index do |el, ind|
if el != final[ind]
swap(0, el)
p @arr
break
end
end
end
@moves
end
end
def garage(beg, final)
fl = FastList.new beg
fl.calc_moves(final)
end
initial = [1, 2, 3, 0, 4]
final = [0, 3, 2, 1, 4]
print 'initial:'
p initial
p(garage(initial, final))
print 'final should be:'
p final