-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16b.php
66 lines (52 loc) · 1.46 KB
/
16b.php
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
<?php
include 'utilities.php';
$dancers = range('a', 'p');
$moves = explode(',', input());
$start = implode('', $dancers);
$rounds = 1000000000;
$rotations = 0;
/**
* @param array $dancers
* @param array $moves
* @return array
*/
function dance(array $dancers, array $moves): array
{
foreach ($moves as $move) {
$action = substr($move, 0, 1);
$info = substr($move, 1);
switch ($action) {
case 's':
for($i=0; $i < $info; $i++) {
array_unshift($dancers, array_pop($dancers));
}
break;
case 'x':
[$a, $b] = explode('/', $info);
$temp = $dancers;
$dancers[$a] = $temp[$b];
$dancers[$b] = $temp[$a];
break;
case 'p':
[$a, $b] = explode('/', $info);
$temp = $dancers;
$key1 = array_search ($a, $dancers);
$key2 = array_search ($b, $dancers);
$dancers[$key1] = $temp[$key2];
$dancers[$key2] = $temp[$key1];
break;
}
}
return $dancers;
}
for ($i=1; $i<$rounds; $i++) {
$dancers = dance($dancers, $moves);
if (implode("", $dancers) === $start) {
$rotations = $i;
break;
}
}
for( $i=0; $i < ($rounds - $rotations) % $rotations; $i++) {
$dancers = dance($dancers, $moves);
}
echo implode($dancers) . PHP_EOL;