3rd and 2nd day
This commit is contained in:
17
01/index.php
17
01/index.php
@@ -1,17 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
$increased = 0;
|
||||
$inputStream = fopen('input', 'r');
|
||||
$previousReading = (int)fgets($inputStream);
|
||||
while (($line = fgets($inputStream)) !== false) {
|
||||
$line = (int)$line;
|
||||
if ($line > $previousReading) {
|
||||
$increased++;
|
||||
}
|
||||
$previousReading = $line;
|
||||
}
|
||||
fclose($inputStream);
|
||||
echo $increased; //1553
|
||||
17
01/part1.php
Normal file
17
01/part1.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
$increased = 0;
|
||||
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
$input = array_map('intval', $input);
|
||||
$previousReading = array_shift($input);
|
||||
|
||||
foreach ($input as $line) {
|
||||
if ($line > $previousReading) {
|
||||
$increased++;
|
||||
}
|
||||
$previousReading = $line;
|
||||
}
|
||||
echo $increased; //1553
|
||||
18
01/part2.php
Normal file
18
01/part2.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
$input = array_map('intval', $input);
|
||||
|
||||
$increased = 0;
|
||||
$previousReading = array_sum(array_slice($input, 0, 3));
|
||||
for ($i = 1; $i < count($input) - 2; $i++) {
|
||||
$currentReading = array_sum(array_slice($input, $i, 3));
|
||||
if ($currentReading > $previousReading) {
|
||||
$increased++;
|
||||
}
|
||||
$previousReading = $currentReading;
|
||||
}
|
||||
echo $increased; // 1597
|
||||
15
02/part1.php
Normal file
15
02/part1.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
$increased = 0;
|
||||
$inputStream = fopen('input', 'r');
|
||||
$horizontal = $depth = 0;
|
||||
|
||||
while (($line = fgets($inputStream)) !== false) {
|
||||
[$instruction, $value] = explode(' ', trim($line));
|
||||
match ($instruction) {
|
||||
'down' => $depth += (int)$value,
|
||||
'up' => $depth -= (int)$value,
|
||||
'forward' => $horizontal += (int)$value,
|
||||
};
|
||||
}
|
||||
echo $depth * $horizontal; // 1635930
|
||||
23
02/part2.php
Normal file
23
02/part2.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
$increased = 0;
|
||||
$inputStream = fopen('input', 'r');
|
||||
$horizontal = $depth = $aim = 0;
|
||||
|
||||
while (($line = fgets($inputStream)) !== false) {
|
||||
[$instruction, $value] = explode(' ', trim($line));
|
||||
$value = (int)$value;
|
||||
switch ($instruction) {
|
||||
case 'down':
|
||||
$aim += $value;
|
||||
break;
|
||||
case 'up':
|
||||
$aim -= $value;
|
||||
break;
|
||||
case 'forward':
|
||||
$horizontal += $value;
|
||||
$depth += $value * $aim;
|
||||
break;
|
||||
}
|
||||
}
|
||||
echo $depth * $horizontal; // 1781819478
|
||||
19
03/part1.php
Normal file
19
03/part1.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
$input = array_map(fn($line) => str_split($line), $input);
|
||||
|
||||
$o2 = $co2 = $input;
|
||||
|
||||
for ($i = 0; $i < count($input[0]); $i++) {
|
||||
if (count($o2) > 1) {
|
||||
$bit = (int)(array_sum($column = array_column($o2, $i)) >= ceil(count($column) / 2));
|
||||
$o2 = array_filter($o2, fn($line) => $line[$i] == $bit);
|
||||
}
|
||||
|
||||
if (count($co2) > 1) {
|
||||
$bit = (int) (array_sum($column = array_column($co2, $i)) >= ceil(count($column) / 2));
|
||||
$co2 = array_filter($co2, fn ($line) => $line[$i] != $bit);
|
||||
}
|
||||
}
|
||||
echo bindec(implode('', reset($co2))) * bindec(implode('', reset($o2)));
|
||||
11
03/part2.php
Normal file
11
03/part2.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
$input = array_map(fn($line) => str_split($line), $input);
|
||||
$gamma = $epsilon = [];
|
||||
for ($i = 0; $i < count($input[0]); $i++) {
|
||||
$bit = (int) (array_sum($column = array_column($input, $i)) >= ceil(count($column) / 2));
|
||||
$gamma[] = $bit;
|
||||
$epsilon[] = ~$bit & 1;
|
||||
}
|
||||
echo bindec(implode('', $gamma)) * bindec(implode('', $epsilon)).PHP_EOL;
|
||||
Reference in New Issue
Block a user