Finit le jour 6

This commit is contained in:
Clement Desmidt 2021-12-06 16:37:21 +01:00
parent f7608a8fc4
commit 10b3cd3730
2 changed files with 43 additions and 0 deletions

19
day_6/part_1.php Normal file
View File

@ -0,0 +1,19 @@
<?php
$timers = explode(',', file_get_contents('./input.txt'));
echo sprintf('Initial state: %s', implode(',', $timers));
for ($i = 0; $i < 80; $i++) {
for($t = 0, $l = count($timers); $t < $l; $t++) {
if ($timers[$t] > 0) {
$timers[$t]--;
} else {
$timers[$t] = 6;
$timers[] = 8;
}
}
echo sprintf('After %u day(s): %s%s', $i + 1, implode(',',$timers), "\n");
}
echo count($timers);

24
day_6/part_2.php Normal file
View File

@ -0,0 +1,24 @@
<?php
$timers = array_map('intval', explode(',', file_get_contents('./input.txt')));
echo sprintf('Initial state: %s', implode(',', $timers));
$timers = array_count_values($timers);
for ($i = 0; $i < 256; $i++) {
$tmp = array_fill(0, 9, 0);
foreach ($timers as $number => $count) {
if($number > 0) {
$tmp[$number-1] += $count;
} else {
$tmp[6] += $count;
$tmp[8] += $count;
}
}
$timers = $tmp;
echo sprintf('After %u day(s): %s with %u zero%s', $i + 1, array_sum($timers), (array_key_exists(0, $timers) ? $timers[0] : 0), "\n");
}
echo array_sum($timers);