This repository was archived by the owner on Jul 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay6.php
More file actions
73 lines (54 loc) · 1.24 KB
/
Day6.php
File metadata and controls
73 lines (54 loc) · 1.24 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
66
67
68
69
70
71
<?php
function isConfigurationKnown($input, &$knownConfigurations)
{
$config = implode(',', $input);
if (in_array($config, $knownConfigurations)) {
return true;
}
$knownConfigurations[] = $config;
return false;
}
function getMaxPosition($input)
{
$maxValue = 0;
$maxKey = 0;
foreach ($input as $key => $value) {
if ($value > $maxValue) {
$maxValue = $value;
$maxKey = $key;
}
}
return $maxKey;
}
$input = '14 0 15 12 11 11 3 5 1 6 8 4 9 1 8 4';
$mem = explode("\t", $input);
$positions = count($mem) -1;
$knownConfigurations = [];
$step = 0;
$loop = false;
while (true) {
if (isConfigurationKnown($mem, $knownConfigurations)) {
if ($loop) {
echo "$step\n";
return;
}
$knownConfigurations = [];
echo "$step\n";
$step = 0;
$loop = true;
isConfigurationKnown($mem, $knownConfigurations);
}
$maxPosition = getMaxPosition($mem);
$val = $mem[$maxPosition];
$mem[$maxPosition] = 0;
$pos = $maxPosition;
while ($val > 0) {
$pos++;
if ($pos > $positions) {
$pos = 0;
}
$mem[$pos]++;
$val--;
}
$step++;
}