Add day 6

This commit is contained in:
Tristan 2021-12-07 09:49:43 +01:00
parent 17b0d7098c
commit 90f8b5e297
Signed by: trizz
GPG Key ID: 0A93DEC67165EB47
6 changed files with 186 additions and 0 deletions

2
aoc21
View File

@ -6,6 +6,7 @@ use AdventOfCode21\Day1;
use AdventOfCode21\Day2;
use AdventOfCode21\Day3;
use AdventOfCode21\Day4;
use AdventOfCode21\Day6;
use AdventOfCode21\Puzzle;
use Symfony\Component\Console\Application;
@ -16,5 +17,6 @@ $application->add(new Day1());
$application->add(new Day2());
$application->add(new Day3());
$application->add(new Day4());
$application->add(new Day6());
$application->run();

1
data/day6/data.txt Normal file
View File

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

1
data/day6/example.txt Normal file
View File

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

68
data/day6/puzzle.md Normal file
View File

@ -0,0 +1,68 @@
# Day 6: Lanternfish
[https://adventofcode.com/2021/day/6](https://adventofcode.com/2021/day/6)
## Description
### Part One
The sea floor is getting steeper. Maybe the sleigh keys got carried this way?
A massive school of glowing [lanternfish](https://en.wikipedia.org/wiki/Lanternfish) swims past. They must spawn quickly to reach such large numbers - maybe _exponentially_ quickly? You should model their growth rate to be sure.
Although you know nothing about this specific species of lanternfish, you make some guesses about their attributes. Surely, <span title="I heard you like lanternfish.">each lanternfish creates a new lanternfish</span> once every _7_ days.
However, this process isn't necessarily synchronized between every lanternfish - one lanternfish might have 2 days left until it creates another lanternfish, while another might have 4. So, you can model each fish as a single number that represents _the number of days until it creates a new lanternfish_.
Furthermore, you reason, a _new_ lanternfish would surely need slightly longer before it's capable of producing more lanternfish: two more days for its first cycle.
So, suppose you have a lanternfish with an internal timer value of `3`:
* After one day, its internal timer would become `2`.
* After another day, its internal timer would become `1`.
* After another day, its internal timer would become `0`.
* After another day, its internal timer would reset to `6`, and it would create a _new_ lanternfish with an internal timer of `8`.
* After another day, the first lanternfish would have an internal timer of `5`, and the second lanternfish would have an internal timer of `7`.
A lanternfish that creates a new fish resets its timer to `6`, _not `7`_ (because `0` is included as a valid timer value). The new lanternfish starts with an internal timer of `8` and does not start counting down until the next day.
Realizing what you're trying to do, the submarine automatically produces a list of the ages of several hundred nearby lanternfish (your puzzle input). For example, suppose you were given the following list:
3,4,3,1,2
This list means that the first fish has an internal timer of `3`, the second fish has an internal timer of `4`, and so on until the fifth fish, which has an internal timer of `2`. Simulating these fish over several days would proceed as follows:
Initial state: 3,4,3,1,2
After 1 day: 2,3,2,0,1
After 2 days: 1,2,1,6,0,8
After 3 days: 0,1,0,5,6,7,8
After 4 days: 6,0,6,4,5,6,7,8,8
After 5 days: 5,6,5,3,4,5,6,7,7,8
After 6 days: 4,5,4,2,3,4,5,6,6,7
After 7 days: 3,4,3,1,2,3,4,5,5,6
After 8 days: 2,3,2,0,1,2,3,4,4,5
After 9 days: 1,2,1,6,0,1,2,3,3,4,8
After 10 days: 0,1,0,5,6,0,1,2,2,3,7,8
After 11 days: 6,0,6,4,5,6,0,1,1,2,6,7,8,8,8
After 12 days: 5,6,5,3,4,5,6,0,0,1,5,6,7,7,7,8,8
After 13 days: 4,5,4,2,3,4,5,6,6,0,4,5,6,6,6,7,7,8,8
After 14 days: 3,4,3,1,2,3,4,5,5,6,3,4,5,5,5,6,6,7,7,8
After 15 days: 2,3,2,0,1,2,3,4,4,5,2,3,4,4,4,5,5,6,6,7
After 16 days: 1,2,1,6,0,1,2,3,3,4,1,2,3,3,3,4,4,5,5,6,8
After 17 days: 0,1,0,5,6,0,1,2,2,3,0,1,2,2,2,3,3,4,4,5,7,8
After 18 days: 6,0,6,4,5,6,0,1,1,2,6,0,1,1,1,2,2,3,3,4,6,7,8,8,8,8
Each day, a `0` becomes a `6` and adds a new `8` to the end of the list, while each other number decreases by 1 if it was present at the start of the day.
In this example, after 18 days, there are a total of `26` fish. After 80 days, there would be a total of _`5934`_.
Find a way to simulate lanternfish. _How many lanternfish would there be after 80 days?_
### Part Two
Suppose the lanternfish live forever and have unlimited food and space. Would they take over the entire ocean?
After 256 days in the example above, there would be a total of _`26984457539`_ lanternfish!
_How many lanternfish would there be after 256 days?_

90
src/Day6.php Normal file
View File

@ -0,0 +1,90 @@
<?php
namespace AdventOfCode21;
use JetBrains\PhpStorm\Immutable;
class Day6 extends AbstractCommand
{
/**
* {@inheritdoc}
*/
protected static int $day = 6;
/**
* @var int[]
* @psalm-param array{int: int}
*/
#[Immutable]
protected array $startState = [
8 => 0,
7 => 0,
6 => 0,
5 => 0,
4 => 0,
3 => 0,
2 => 0,
1 => 0,
0 => 0,
];
/**
* {@inheritdoc}
*/
protected function part1(array $data): int
{
return $this->processPuzzle(80, $data[0]);
}
/**
* {@inheritdoc}
*/
protected function part2(array $data): int
{
return $this->processPuzzle(256, $data[0]);
}
/**
* @param int[] $state
* @psalm-param array{int: int} $state
*
* @return array
*/
protected function processDay(array $state): array
{
$newState = $state;
/**
* @var int $key
* @var int $stateValue
*/
foreach ($state as $key => $stateValue) {
$newKey = $key - 1;
if ($newKey < 0) {
$newState[8] = $stateValue;
$newKey = 6;
}
$newState[$key] -= $stateValue;
$newState[$newKey] += $stateValue;
}
return $newState;
}
protected function processPuzzle(int $numberOfDays, string $data): int
{
$state = $this->startState;
array_map(static function (string $stateValue) use (&$state) {
++$state[(int) $stateValue];
}, explode(',', $data));
for ($day = 0; $day < $numberOfDays; ++$day) {
$state = $this->processDay($state);
}
return (int) array_sum($state);
}
}

24
tests/Day6Test.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace Tests;
use AdventOfCode21\Day6;
/**
* @internal
*/
class Day6Test extends AbstractTestCase
{
public static int $part1ExampleResult = 5934;
public static int $part1Result = 350917;
public static int $part2ExampleResult = 26984457539;
public static int $part2Result = 1592918715629;
public function setupDay(): Day6
{
return new class() extends Day6 {
use ReturnTestableResults;
};
}
}