✨ Finit les 3e et 4e jours
This commit is contained in:
36
day_3/part_2.php
Normal file
36
day_3/part_2.php
Normal 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);
|
||||
Reference in New Issue
Block a user