-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.2.php
56 lines (38 loc) · 1.05 KB
/
14.2.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
<?php
$input = '846021';
function move_elf(&$elf, $scoreboard) {
$move_to = $elf + substr($scoreboard, $elf, 1) + 1;
if ($move_to >= strlen($scoreboard)) {
$elf = ($move_to % strlen($scoreboard));
} else {
$elf = $move_to;
}
}
$scoreboard = 37;
$elves = [
0 => 0,
1 => 1,
];
$input_length = strlen($input);
$last_six_recipes = '';
while (true) {
$new_recipe = substr($scoreboard, $elves[0], 1) + substr($scoreboard, $elves[1], 1);
$next_letter = substr($new_recipe, 0, 1);
$last_six_recipes = substr($last_six_recipes . $next_letter, 0 - $input_length, $input_length);
$scoreboard .= $next_letter;
if ($input === $last_six_recipes) {
break;
}
if ($new_recipe > 9) {
$next_letter = substr($new_recipe, 1, 1);
$last_six_recipes = substr($last_six_recipes . $next_letter, 0 - $input_length, $input_length);
$scoreboard .= $next_letter;
if ($input === $last_six_recipes) {
break;
}
}
foreach ($elves as &$elf) {
move_elf($elf, $scoreboard);
}
}
echo strlen($scoreboard) - $input_length;