-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17.php
53 lines (45 loc) · 1.26 KB
/
day17.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
<?php
/**
--- Day 17: No Such Thing as Too Much ---
The elves bought too much eggnog again - 150 liters this time. To fit it all
into your refrigerator, you'll need to move it into smaller containers. You take
an inventory of the capacities of the available containers.
For example, suppose you have containers of size 20, 15, 10, 5, and 5 liters.
If you need to store 25 liters, there are four ways to do it:
15 and 10
20 and 5 (the first 5)
20 and 5 (the second 5)
15, 5, and 5
Filling all containers entirely, how many different combinations of containers
can exactly fit all 150 liters of eggnog?
*/
const TEST = false;
if (TEST) {
$containers = [20,15,10,5,5];
$amount = 25;
} else {
$containers = [33,14,18,20,45,35,16,35,1,13,18,13,50,44,48,6,24,41,30,42];
sort($containers);
$amount = 150;
}
$score = 0;
$counts = [];
function fill($amount, $jars, $count=0) {
if ($amount === 0) {
global $score, $counts;
$counts[$count]++;
$score++;
return;
}
for ($i=0; $i<count($jars); $i++) {
$rest = array_slice($jars, $i+1);
if ($amount >= $jars[$i]) {
fill($amount-$jars[$i], $rest, $count+1);
}
}
}
fill($amount, $containers);
print("Combinations: ${score}\n");
sort($counts);
$min = array_shift($counts);
print("Minimum: ${min}\n");