-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
85 lines (67 loc) · 1.89 KB
/
index.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
function permutations($values, $size)
{
$a = array();
$c = pow(count($values), $size);
for ($i = 0; $i < $c; $i++) {
$array = array();
$count = count($values);
for ($j = 0; $j < $size; $j++) {
$selector = ($i / pow($count, $j)) % $count;
$array[$j] = $values[$selector];
}
// $a[$i] = $array;
yield $array;
}
// return $a;
}
function numbersReachSum($target, $days, $maxHours)
{
$results = [];
foreach (permutations(range(0, $maxHours), $days) as $permutation) {
if (array_sum($permutation) == $target) {
array_push(
$results,
implode(',', $permutation)
);
}
}
return $results;
};
function findSchedules($workHours, $dayHours, $pattern)
{
// if ($workHours == 56) {
// return ['8888888'];
// }
$result = [];
$maxHours = 8;
$arrayPattern = str_split($pattern);
$missing_days = count(array_keys($arrayPattern, "?"));
$openDays = array_keys($arrayPattern, "?");
$func = function ($value) {
if ($value == '?') {
return str_replace('?', '', $value);
}
return (int)$value;
};
$worked = array_sum(array_map($func, $arrayPattern));
$dif = $workHours - $worked;
if ($dif <= 8) {
$maxHours = $dif;
}
$permuts = numbersReachSum($dif, $missing_days, $dayHours);
foreach ($permuts as $key =***REMOVED*** $permut) {
$newPattern = array_replace(
$arrayPattern,
array_combine($openDays, explode(',', $permuts[$key]))
);
array_push($result, implode('', $newPattern));
}
// sort($result);
echo '<pre***REMOVED***';
print_r($result);
echo '</pre***REMOVED***';
};
findSchedules(56, 8, '???8???');
// findSchedules(3, 2, '??2??00');
// findSchedules(3, 1, '???????');