✨ Finit le jour 2 et commence le 1 et le 3
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
input.txt
|
||||
/.idea
|
||||
19
add.sh
Executable file
19
add.sh
Executable file
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/zsh
|
||||
|
||||
SCRIPT_PATH="$( cd "$(dirname "$0")" ; pwd -P )"
|
||||
NUMBER=$1
|
||||
|
||||
if [ ! -d "${SCRIPT_PATH}/day_${NUMBER}" ]; then
|
||||
mkdir "${SCRIPT_PATH}/day_${NUMBER}"
|
||||
else
|
||||
echo "folder ${SCRIPT_PATH}/day_${NUMBER} already exists"
|
||||
exit
|
||||
fi
|
||||
|
||||
cd "${SCRIPT_PATH}/day_${NUMBER}" || exit
|
||||
|
||||
touch input.txt part_1.php part_2.php
|
||||
|
||||
echo "DONE"
|
||||
|
||||
exit
|
||||
35
day_1/part_1.php
Normal file
35
day_1/part_1.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
$lines = array_filter(explode("\n", file_get_contents(__DIR__.'/input.txt')), static fn($line) =>!empty($line));
|
||||
|
||||
$count = 0;
|
||||
$position = 50;
|
||||
|
||||
echo 'The dial starts by pointing at 50'."\n";
|
||||
|
||||
function checkPosition(&$position)
|
||||
{
|
||||
if ($position < 0) {
|
||||
$position = 100 - abs($position);
|
||||
} elseif ($position > 99) {
|
||||
$position = $position - 100;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($lines as $line) {
|
||||
$direction = substr($line, 0, 1);
|
||||
$number = (int)substr($line, 1) % 100;
|
||||
if ($direction === 'L') {
|
||||
$position -= $number;
|
||||
checkPosition($position);
|
||||
} elseif ($direction === 'R') {
|
||||
$position += $number;
|
||||
checkPosition($position);
|
||||
}
|
||||
|
||||
if ($position === 0) {
|
||||
$count++;
|
||||
}
|
||||
|
||||
echo sprintf('The dial is rotated %s to point at %u | count : %u%s', $line,$position, $count, "\n");
|
||||
}
|
||||
28
day_2/part_1.php
Normal file
28
day_2/part_1.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
$intervals = array_map(fn($interval) => explode('-', $interval), explode(',', file_get_contents('./input.txt')));
|
||||
|
||||
$invalids = [];
|
||||
|
||||
foreach ($intervals as $interval) {
|
||||
[$start, $end] = $interval;
|
||||
$current = $start;
|
||||
while ($current <= $end) {
|
||||
if (is_invalid($current)) {
|
||||
$invalids[] = $current;
|
||||
}
|
||||
|
||||
$current++;
|
||||
}
|
||||
}
|
||||
|
||||
function is_invalid($current): bool {
|
||||
if (strlen($current) % 2 !== 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$middle = strlen($current) / 2;
|
||||
return substr($current, 0, $middle) === substr($current, $middle);
|
||||
}
|
||||
|
||||
echo array_sum($invalids);
|
||||
37
day_2/part_2.php
Normal file
37
day_2/part_2.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
$intervals = array_map(fn($interval) => explode('-', $interval), explode(',', file_get_contents('./input.txt')));
|
||||
|
||||
$invalids = [];
|
||||
|
||||
foreach ($intervals as $interval) {
|
||||
[$start, $end] = $interval;
|
||||
$current = $start;
|
||||
while ($current <= $end) {
|
||||
if (is_invalid($current)) {
|
||||
$invalids[] = $current;
|
||||
}
|
||||
|
||||
$current++;
|
||||
}
|
||||
}
|
||||
|
||||
function is_invalid(string $current): bool {
|
||||
for ($i = 1, $l = strlen($current); $i < $l; $i++) {
|
||||
$parts = str_split($current, $i);
|
||||
$is_equals = true;
|
||||
for ($j = 0, $m = count($parts) - 1; $j < $m; $j++) {
|
||||
if ($parts[$j] !== $parts[$j + 1]) {
|
||||
$is_equals = false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if ($is_equals) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
echo array_sum($invalids);
|
||||
18
day_3/part_1.php
Normal file
18
day_3/part_1.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
$banks = explode("\n", file_get_contents('./input.txt'));
|
||||
|
||||
$output_joltage = [];
|
||||
|
||||
foreach ($banks as $bank) {
|
||||
$a_bank = str_split($bank);
|
||||
|
||||
$max = max(array_slice($a_bank,0,count($a_bank)-1));
|
||||
$i = array_search($max, $a_bank);
|
||||
$right_part = array_slice($a_bank, $i+1);
|
||||
$max2 = max($right_part);
|
||||
|
||||
$output_joltage[] = (int) $max.$max2;
|
||||
}
|
||||
|
||||
echo array_sum($output_joltage);
|
||||
Reference in New Issue
Block a user