day06: Solve puzzles

main
Elis Hirwing 10 months ago
parent 3df8fdc687
commit 84a3c790c5
Signed by: etu
GPG Key ID: D57EFA625C9A925F
  1. 2
      Makefile
  2. 1
      day06/example.txt
  3. 1
      day06/input.txt
  4. 33
      day06/lib.php
  5. 39
      day06/solve.php

@ -18,6 +18,8 @@ day05:
php day05/solve.php input
day06:
php day06/solve.php input
day07:
day08:
day09:

@ -0,0 +1 @@
3,4,3,1,2

@ -0,0 +1 @@
2,1,1,4,4,1,3,4,2,4,2,1,1,4,3,5,1,1,5,1,1,5,4,5,4,1,5,1,3,1,4,2,3,2,1,2,5,5,2,3,1,2,3,3,1,4,3,1,1,1,1,5,2,1,1,1,5,3,3,2,1,4,1,1,1,3,1,1,5,5,1,4,4,4,4,5,1,5,1,1,5,5,2,2,5,4,1,5,4,1,4,1,1,1,1,5,3,2,4,1,1,1,4,4,1,2,1,1,5,2,1,1,1,4,4,4,4,3,3,1,1,5,1,5,2,1,4,1,2,4,4,4,4,2,2,2,4,4,4,2,1,5,5,2,1,1,1,4,4,1,4,2,3,3,3,3,3,5,4,1,5,1,4,5,5,1,1,1,4,1,2,4,4,1,2,3,3,3,3,5,1,4,2,5,5,2,1,1,1,1,3,3,1,1,2,3,2,5,4,2,1,1,2,2,2,1,3,1,5,4,1,1,5,3,3,2,2,3,1,1,1,1,2,4,2,2,5,1,2,4,2,1,1,3,2,5,5,3,1,3,3,1,4,1,1,5,5,1,5,4,1,1,1,1,2,3,3,1,2,3,1,5,1,3,1,1,3,1,1,1,1,1,1,5,1,1,5,5,2,1,1,5,2,4,5,5,1,1,5,1,5,5,1,1,3,3,1,1,3,1

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
function buildFishIndex(array $fishes) : array
{
$index = [];
foreach ($fishes as $val) {
$index[$val] = ($index[$val] ?? 0) + 1;
}
return $index;
}
function decreaseFishesIndex(array $fishIndex) : array
{
$newIndex = [];
foreach ($fishIndex as $days => $amount) {
$newIndex[intval($days - 1)] = intval($amount);
}
foreach ($newIndex as $day => $amount) {
if ($day < 0) {
unset($newIndex[$day]);
$newIndex[8] = $amount;
$newIndex[6] = ($newIndex[6] ?? 0) + $amount;
}
}
return $newIndex;
}

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
require_once(__DIR__.'/../lib/functions.php');
require_once(__DIR__.'/lib.php');
$entries = getLines(__DIR__.'/'.$argv[1].'.txt');
timedPrintf(
'[2021-12-06.1] Amount of fishes after 80 days: %d [Time: %ss]',
(function (array $entries) : int {
$fishes = explode(',', $entries[0]);
$fishIndex = buildFishIndex($fishes);
for ($i = 1; $i <= 80; $i++) {
$fishIndex = decreaseFishesIndex($fishIndex);
}
return array_sum($fishIndex);
}),
$entries
);
timedPrintf(
'[2021-12-06.2] Amount of fishes after 256 days: %d [Time: %ss]',
(function (array $entries) : int {
$fishes = explode(',', $entries[0]);
$fishIndex = buildFishIndex($fishes);
for ($i = 1; $i <= 256; $i++) {
$fishIndex = decreaseFishesIndex($fishIndex);
}
return array_sum($fishIndex);
}),
$entries
);
Loading…
Cancel
Save