🎉 Hello world
This commit is contained in:
commit
4cefe61056
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
input.txt
|
||||
/.idea
|
9
day_1/part_1.php
Normal file
9
day_1/part_1.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
$masses = explode(',', file_get_contents('input.txt'));
|
||||
|
||||
echo array_sum(
|
||||
array_map(function($mass) {
|
||||
return floor($mass / 3) - 2;
|
||||
}, $masses)
|
||||
);
|
19
day_1/part_2.php
Normal file
19
day_1/part_2.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
$masses = explode(',', file_get_contents('input.txt'));
|
||||
|
||||
function get_fuel_from_mass($mass)
|
||||
{
|
||||
return floor($mass / 3) - 2;
|
||||
}
|
||||
|
||||
echo array_sum(
|
||||
array_map(function($mass) {
|
||||
$total_fuel = 0;
|
||||
while($mass > 0) {
|
||||
$mass = max(get_fuel_from_mass($mass), 0);
|
||||
$total_fuel += $mass;
|
||||
}
|
||||
return $total_fuel;
|
||||
}, $masses)
|
||||
);
|
26
day_2/part_1.php
Normal file
26
day_2/part_1.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
$input = explode(',', file_get_contents('input.txt'));
|
||||
|
||||
// don't forget : 12 @ position 1 && 2 @ position 2
|
||||
|
||||
$i = 0;
|
||||
while($input[$i] !== 99) {
|
||||
[$operation, $first, $second, $position] = array_slice($input, $i, 4);
|
||||
switch ($operation) {
|
||||
case 1:
|
||||
$input[$position] = $input[$first] + $input[$second];
|
||||
break;
|
||||
case 2:
|
||||
$input[$position] = $input[$first] * $input[$second];
|
||||
break;
|
||||
case 99:
|
||||
echo $input[0];
|
||||
exit;
|
||||
default:
|
||||
echo 'unknown operation : '.$operation;
|
||||
exit;
|
||||
}
|
||||
$i += 4;
|
||||
}
|
||||
|
||||
echo $input[0];
|
46
day_2/part_2.php
Normal file
46
day_2/part_2.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
$input = explode(',', file_get_contents('input.txt'));
|
||||
|
||||
function run_opcode(array $input)
|
||||
{
|
||||
$i = 0;
|
||||
while ($input[$i] !== 99) {
|
||||
[$operation, $first, $second, $position] = array_slice($input, $i, 4);
|
||||
switch ($operation) {
|
||||
case 1:
|
||||
$input[$position] = $input[$first] + $input[$second];
|
||||
break;
|
||||
case 2:
|
||||
$input[$position] = $input[$first] * $input[$second];
|
||||
break;
|
||||
case 99:
|
||||
return $input[0];
|
||||
default:
|
||||
echo 'unknown operation : '.$operation;
|
||||
exit;
|
||||
}
|
||||
$i += 4;
|
||||
}
|
||||
|
||||
return $input[0];
|
||||
}
|
||||
|
||||
$noun = 0;
|
||||
$verb = 0;
|
||||
while ($verb !== 99) {
|
||||
$noun = 0;
|
||||
while ($noun !== 99) {
|
||||
$clone_input = $input;
|
||||
$clone_input[1] = $noun;
|
||||
$clone_input[2] = $verb;
|
||||
$magic_number = run_opcode($clone_input);
|
||||
if ($magic_number === 19690720) {
|
||||
echo sprintf('noun : %s | verb : %s', $noun, $verb);
|
||||
}
|
||||
$noun++;
|
||||
}
|
||||
$verb++;
|
||||
}
|
||||
|
||||
echo sprintf("\n".'answer : %u', ((100*$noun)+$verb));
|
59
day_4/part_1.php
Normal file
59
day_4/part_1.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
[$min, $max] = explode('-', file_get_contents('input.txt'));
|
||||
|
||||
/**
|
||||
* @param int|string $big_number
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function has_at_least_two_adjacent_number($big_number)
|
||||
{
|
||||
$big_number = (string) $big_number;
|
||||
$current_number = null;
|
||||
for ($i = 0, $l = strlen($big_number); $i < $l; $i++) {
|
||||
if ($current_number === $big_number[$i]) {
|
||||
return true;
|
||||
}
|
||||
$current_number = $big_number[$i];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|string $big_number
|
||||
* @return bool
|
||||
*/
|
||||
function never_decrease($big_number)
|
||||
{
|
||||
$big_number = (string) $big_number;
|
||||
$current_number = null;
|
||||
for ($i = 0, $l = strlen($big_number); $i < $l; $i++) {
|
||||
if ($current_number > $big_number[$i]) {
|
||||
return false;
|
||||
}
|
||||
$current_number = $big_number[$i];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|string $number
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function is_valid($number)
|
||||
{
|
||||
return has_at_least_two_adjacent_number($number) && never_decrease($number);
|
||||
}
|
||||
|
||||
$valids = [];
|
||||
for ($i = $min; $i <= $max; $i++) {
|
||||
if (is_valid($i)) {
|
||||
$valids[] = $i;
|
||||
}
|
||||
}
|
||||
|
||||
echo count($valids);
|
63
day_4/part_2.php
Normal file
63
day_4/part_2.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
[$min, $max] = explode('-', file_get_contents('input.txt'));
|
||||
|
||||
/**
|
||||
* @param int|string $big_number
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function has_no_more_than_two_adjacent_number($big_number)
|
||||
{
|
||||
$big_number = (string) $big_number;
|
||||
$current_numbers = [];
|
||||
for ($i = 0, $l = strlen($big_number); $i < $l; $i++) {
|
||||
if (!in_array($big_number[$i], $current_numbers, true)) {
|
||||
if (count($current_numbers) === 2) {
|
||||
return true;
|
||||
}
|
||||
$current_numbers = [$big_number[$i]];
|
||||
} else {
|
||||
$current_numbers[] = $big_number[$i];
|
||||
}
|
||||
}
|
||||
|
||||
return count($current_numbers) === 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|string $big_number
|
||||
* @return bool
|
||||
*/
|
||||
function never_decrease($big_number)
|
||||
{
|
||||
$big_number = (string) $big_number;
|
||||
$current_number = null;
|
||||
for ($i = 0, $l = strlen($big_number); $i < $l; $i++) {
|
||||
if ($current_number > $big_number[$i]) {
|
||||
return false;
|
||||
}
|
||||
$current_number = $big_number[$i];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|string $number
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function is_valid($number)
|
||||
{
|
||||
return has_no_more_than_two_adjacent_number($number) && never_decrease($number);
|
||||
}
|
||||
|
||||
$valids = [];
|
||||
for ($i = $min; $i <= $max; $i++) {
|
||||
if (is_valid($i)) {
|
||||
$valids[] = $i;
|
||||
}
|
||||
}
|
||||
|
||||
echo count($valids); // more than 574 & less than 1099
|
Loading…
Reference in New Issue
Block a user