Finit les 3e et 4e jours

This commit is contained in:
Clement Desmidt
2025-12-11 14:51:22 +01:00
parent 8b73ada230
commit 746ab0457a
3 changed files with 90 additions and 0 deletions

36
day_3/part_2.php Normal file
View File

@@ -0,0 +1,36 @@
<?php
$banks = explode("\n", file_get_contents('./input.txt'));
$output_joltage = [];
foreach ($banks as $bank) {
$a_bank = str_split($bank);
$a_bank_length = count($a_bank);
$max_index = $a_bank_length - 12;
$min_index = 0;
$bank_joltage = [];
while (count($bank_joltage) < 12) {
$slice = array_slice($a_bank, 0, $max_index);
$max = max($slice);
$min_index = array_search($max, $a_bank);
$bank_joltage[] = $max;
array_splice($a_bank, 0, $min_index+1);
if (count($a_bank) + count($bank_joltage) === 12) {
$bank_joltage = array_merge($bank_joltage, $a_bank);
}
$a_bank_length = count($a_bank);
$max_index = $a_bank_length - 11 + count($bank_joltage);
}
$largest_voltage = (int)implode('', $bank_joltage);
$output_joltage[] = $largest_voltage;
echo $largest_voltage . "\n";
}
echo array_sum($output_joltage);

54
day_4/part_1.php Normal file
View File

@@ -0,0 +1,54 @@
<?php
$grid = array_map(fn($line) => str_split($line), array_filter(explode("\n", file_get_contents(__DIR__.'/input.txt')), static fn($line) =>!empty($line)));
$count = 0;
do {
$local_count = 0;
$removed_papers = [];
foreach ($grid as $i => $line) {
foreach ($line as $j => $cell) {
switch ($cell) {
case '@':
if (count_adjacents($i, $j) < 4) {
$local_count++;
$removed_papers[] = [$i, $j];
}
break;
case '.':
default:
break;
}
}
}
foreach ($removed_papers as $removed_paper) {
$grid[$removed_paper[0]][$removed_paper[1]] = '.';
}
$count += $local_count;
} while ($local_count !== 0);
echo $count;
function count_adjacents(int $i, int $j): int
{
global $grid;
$count = 0;
if (array_key_exists($i - 1, $grid)) {
if (array_key_exists($j - 1, $grid[$i - 1]) && $grid[$i - 1][$j - 1] === '@') $count++;
if ($grid[$i - 1][$j] === '@') $count++;
if (array_key_exists($j + 1, $grid[$i - 1]) && $grid[$i - 1][$j + 1] === '@') $count++;
}
if (array_key_exists($j - 1, $grid[$i]) && $grid[$i][$j - 1] === '@') $count++;
if (array_key_exists($j + 1, $grid[$i]) && $grid[$i][$j + 1] === '@') $count++;
if (array_key_exists($i + 1, $grid)) {
if (array_key_exists($j - 1, $grid[$i + 1]) && $grid[$i + 1][$j - 1] === '@') $count++;
if ($grid[$i + 1][$j] === '@') $count++;
if (array_key_exists($j + 1, $grid[$i + 1]) && $grid[$i + 1][$j + 1] === '@') $count++;
}
return $count;
}

0
day_4/part_2.php Normal file
View File