-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay6.php
71 lines (54 loc) · 1.57 KB
/
Day6.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
declare(strict_types=1);
namespace XonneX\AdventOfCode\Y2021\Solutions\Day6;
use XonneX\AdventOfCode\Core\AbstractSolution;
use function array_fill;
use function array_sum;
use function count;
use function explode;
class Day6 extends AbstractSolution
{
public function __construct()
{
parent::__construct(2021, 6);
}
protected function partOne(string $input): string
{
$state = explode(',', $input);
foreach ($state as $key => $value) {
$state[$key] = (int) $value;
}
for ($days = 0; $days < 80; $days++) {
foreach ($state as $key => $fishCount) {
$fishCount--;
if ($fishCount < 0) {
$state[] = 8;
$fishCount = 6;
}
$state[$key] = $fishCount;
}
}
return (string) count($state);
}
protected function partTwo(string $input): string
{
$states = explode(',', $input);
$counts = array_fill(0, 9, 0);
foreach ($states as $state) {
$counts[$state]++;
}
for ($i = 0; $i < 256; $i++) {
$newCounts = array_fill(0, 9, 0);
foreach ($counts as $key => $count) {
if ($key === 0) {
$newCounts[6] += $count;
$newCounts[8] += $count;
} else {
$newCounts[$key - 1] += $count;
}
}
$counts = $newCounts;
}
return (string) array_sum($counts);
}
}