-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday17.php
More file actions
executable file
·48 lines (36 loc) · 985 Bytes
/
day17.php
File metadata and controls
executable file
·48 lines (36 loc) · 985 Bytes
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
<?php
ini_set("memory_limit","256M");
function binToGalons($bin, $boxis) {
$revertedBin = strrev($bin);
$galons = 0;
for ($i = 0 ; $i < strlen($revertedBin); $i++) {
$value = $revertedBin[$i];
if ((int)$value === 1) {
$galons += $boxis[(count($boxis) - 1 - $i)];
}
}
return $galons;
}
$inputFile = fopen("input17.txt",'r');
$boxis = array();
$maxCombinationBin = '';
while ($row = trim(fgets($inputFile))) {
$boxis[] = (int)$row;
$maxCombinationBin .= '1';
}
fclose($inputFile);
$maxCombinationDec = bindec($maxCombinationBin);
$combination = array();
for ($i=1; $i <= $maxCombinationDec ; $i++) {
$bin = decbin($i);
$galons = binToGalons($bin, $boxis);
if ($galons == 150) {
$key = strlen(str_replace('0', '', $bin));
if (isset($combination[$key])) {
$combination[$key]++;
} else {
$combination[$key] = 1;
}
}
}
var_dump($combination);