-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdance.js
45 lines (40 loc) · 1.5 KB
/
dance.js
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
const { SPIN, EXCHANGE, PARTNER } = require('./input');
class Dance {
constructor(steps, programs = 'abcdefghijklmnop') {
this.programs = programs.split('');
this.steps = JSON.parse(JSON.stringify(steps));
}
run() {
this.steps.forEach(step => {
let { move, arg } = step;
if (move === SPIN) {
// Cut off the last N elements, and move them to beginning
let slice_count = arg;
let last_n_programs = this.programs.slice(-1 * slice_count);
this.programs = last_n_programs.concat(
this.programs.slice(0, this.programs.length - slice_count)
);
} else if (move === EXCHANGE) {
// Swap elements at positions `a` and `b`
let [a, b] = arg;
let temp_a = this.programs[a];
this.programs[a] = this.programs[b];
this.programs[b] = temp_a;
} else if (move === PARTNER) {
// Swap elements `a` and `b`, and swap them
let [el_a, el_b] = arg;
let a = this.programs.indexOf(el_a);
let b = this.programs.indexOf(el_b);
this.programs[a] = el_b;
this.programs[b] = el_a;
} else {
let error = `Unknown move "${move}`;
throw error;
}
});
}
getOrder() {
return this.programs.join('');
}
}
module.exports = Dance;