-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathgarage.js
More file actions
65 lines (60 loc) · 1.41 KB
/
garage.js
File metadata and controls
65 lines (60 loc) · 1.41 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
56
57
58
59
60
61
62
63
64
65
class FastList {
constructor(arr) {
this.arr = arr
// Dict for O(1) lookups by value
const indexof = {}
arr.forEach((v, i) => {
indexof[v] = i
})
this.indexof = indexof
}
swap(e1, e2) {
const i1 = this.indexof[e1]
const i2 = this.indexof[e2]
// Set element where 0 is to final element
this.arr[i1] = e2
// Update dict
this.indexof[e2] = i1
// Set 0 where the previous number was
this.arr[i2] = e1
// Update dict
this.indexof[e1] = i2
this.moves += 1
}
calcMoves(end) {
this.moves = 0
while (!this.equals(end)) {
const i0 = this.indexof[0]
if (end[i0] !== 0) {
// If element can be moved to its final position
this.swap(0, end[i0])
console.log(this.arr)
continue
}
for (let ind = 0; ind < this.arr.length; ind++) {
const el = this.arr[ind]
if (el !== end[ind]) {
this.swap(0, el)
console.log(this.arr)
break
}
}
}
return this.moves
}
equals(other) {
return (
this.arr.length === other.length &&
this.arr.every((v, i) => v === other[i])
)
}
}
function garage(beg, end) {
const fl = new FastList(beg)
return fl.calcMoves(end)
}
const initial = [1, 2, 3, 0, 4]
const final = [0, 3, 2, 1, 4]
console.log('initial:', initial)
console.log(garage(initial, final))
console.log('final should be:', final)