-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay3.php
122 lines (96 loc) · 3.44 KB
/
Day3.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
declare(strict_types=1);
namespace XonneX\AdventOfCode\Y2021\Solutions\Day3;
use XonneX\AdventOfCode\Core\AbstractSolution;
use function array_pop;
use function array_values;
use function bindec;
use function count;
use function explode;
use function str_split;
use function strlen;
use function substr_count;
class Day3 extends AbstractSolution
{
public function __construct()
{
parent::__construct(2021, 3);
}
protected function partOne(string $input): string
{
$numbers = explode("\n", $input);
$columnNumbers = [];
$columnCount = strlen($numbers[0]);
for ($i = 0; $i < $columnCount; $i++) {
$columnNumbers[$i] = '';
}
foreach ($numbers as $number) {
$bits = str_split($number);
foreach ($bits as $key => $bit) {
$columnNumbers[$key] .= $bit;
}
}
$gammaRate = '';
$epsilonRate = '';
foreach ($columnNumbers as $columnNumber) {
$zeros = substr_count($columnNumber, '0');
$ones = substr_count($columnNumber, '1');
if ($zeros > $ones) {
$gammaRate .= '0';
$epsilonRate .= '1';
} else {
$gammaRate .= '1';
$epsilonRate .= '0';
}
}
return (string) (bindec($gammaRate) * bindec($epsilonRate));
}
protected function partTwo(string $input): string
{
$numbers = explode("\n", $input);
$oxygenRatingNumbers = array_values($numbers);
$i = 0;
while (count($oxygenRatingNumbers) > 1) {
$columnNumber = $this->getColumnNumber($oxygenRatingNumbers, $i);
$zeros = substr_count($columnNumber, '0');
$ones = substr_count($columnNumber, '1');
foreach ($oxygenRatingNumbers as $oxygenKey => $oxygenRatingNumber) {
$bit = str_split($oxygenRatingNumber)[$i];
if ($zeros > $ones && $bit === '1') {
unset($oxygenRatingNumbers[$oxygenKey]);
} elseif (($zeros < $ones || $zeros === $ones) && $bit === '0') {
unset($oxygenRatingNumbers[$oxygenKey]);
}
}
$i++;
}
$oxygenRating = bindec(array_pop($oxygenRatingNumbers));
$co2RatingNumbers = array_values($numbers);
$i = 0;
while (count($co2RatingNumbers) > 1) {
$columnNumber = $this->getColumnNumber($co2RatingNumbers, $i);
$zeros = substr_count($columnNumber, '0');
$ones = substr_count($columnNumber, '1');
foreach ($co2RatingNumbers as $oxygenKey => $oxygenRatingNumber) {
$bit = str_split($oxygenRatingNumber)[$i];
if (($zeros < $ones || $zeros === $ones) && $bit === '1') {
unset($co2RatingNumbers[$oxygenKey]);
} elseif ($zeros > $ones && $bit === '0') {
unset($co2RatingNumbers[$oxygenKey]);
}
}
$i++;
}
$co2Rating = bindec(array_pop($co2RatingNumbers));
return (string) ($oxygenRating * $co2Rating);
}
private function getColumnNumber(array $numbers, int $column): string
{
$columnNumber = '';
foreach ($numbers as $number) {
$bits = str_split($number);
$columnNumber .= $bits[$column];
}
return $columnNumber;
}
}