-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.php
87 lines (73 loc) · 2.14 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
require_once './AbstractBenchmarking.php';
class FishSchool
{
public int $lanternFishCount = 0;
public array $lanternFish = [];
protected function simulateDay(): array
{
$spawningFish = array_shift($this->lanternFish);
$this->lanternFish[6] += $spawningFish;
$this->lanternFish[] = $spawningFish;
return $this->lanternFish;
}
/**
* @param $data
*/
protected function addLanternFish($data)
{
$this->lanternFish = array_fill(0, 9, 0);
foreach ($data as $age) {
$this->lanternFish[$age]++;
}
}
/**
* @param int $days
* @param mixed $data
*/
public function simulateReplication(int $days, mixed $data)
{
$this->addLanternFish($data);
for ($i = 0; $i < $days; $i++) {
$this->simulateDay();
}
$this->lanternFishCount = array_sum($this->lanternFish);
}
}
class Day6 extends AbstractBenchmarking
{
private array $data;
private array $testData;
private string $rawData;
private string $rawTestData;
public function __construct()
{
$this->rawData = file_get_contents('Data/day6.txt');
$this->rawTestData = file_get_contents('TestData/day6.txt');
$this->parseData(true);
$this->parseData();
}
public function parseData(bool $test = false): void
{
$rawInputData = $test ? $this->rawTestData : $this->rawData;
if ($test) {
$this->testData = explode(',', $rawInputData);
} else {
$this->data = explode(',', $rawInputData);
}
}
public function part1(bool $test = false): int
{
$inputData = $test ? $this->testData : $this->data;
$fishSchool = new FishSchool();
$fishSchool->simulateReplication(80, $inputData);
return $fishSchool->lanternFishCount;
}
public function part2(bool $test = false): int
{
$inputData = $test ? $this->testData : $this->data;
$fishSchool = new FishSchool();
$fishSchool->simulateReplication(256, $inputData);
return $fishSchool->lanternFishCount;
}
}