-
Notifications
You must be signed in to change notification settings - Fork 0
/
3b.php
41 lines (36 loc) · 917 Bytes
/
3b.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
<?php
include "helpers.php";
$input = file('inputs/test/3.txt');
$input = file('inputs/3.txt');
$ogr = calculate($input,0);
$co2 = calculate($input,1);
echo bindec($ogr) * bindec($co2);
function calculate($input, $mask)
{
$length = strlen(trim($input[0]));
for ($i = 0; $i < $length; $i++ ){
$ones = 0;
foreach($input as $index1 => $line){
if((int)$line[$i] == 1){
$ones++;
}
}
if($ones >= sizeof($input) / 2) {
$input = filterInput($input, $i, $mask);
}else{
$input = filterInput($input, $i, $mask ? '0' : 1);
}
if(sizeof($input) == 1){
return $input[0];
}
}
}
function filterInput($input, $bit, $val){
$retArr = [];
foreach ($input as $line){
if((int)$line[$bit] == $val){
$retArr[] = $line;
}
}
return $retArr;
}