-
Notifications
You must be signed in to change notification settings - Fork 0
/
d06_1_2.php
40 lines (30 loc) · 842 Bytes
/
d06_1_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
<?php
$banks = [5, 1, 10, 0, 1, 7, 13, 14, 3, 12, 8, 10, 7, 12, 0, 6];
$hashes = [ get_array_hash($banks) ];
$steps = 0;
while (1) {
$steps++;
$max = max($banks);
$key = array_search($max, $banks);
$counter = 1;
$banks[$key] = 0;
while ($max > 0) {
$index = $key + $counter++;
// Wrap around
if ($index >= count($banks)) $index = $index % count($banks);
$banks[$index]++;
$max--;
}
$arr_hash = get_array_hash($banks);
if (in_array($arr_hash, $hashes)) {
// How many steps between the 2 times the banks look alike
var_dump(count($hashes) - array_search($arr_hash, $hashes));
var_dump($steps);
break;
}
array_push($hashes, $arr_hash);
}
function get_array_hash($arr)
{
return hash('sha256', implode($arr, '-'));
}