-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay1.php
82 lines (57 loc) · 1.69 KB
/
Day1.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
<?php
declare(strict_types=1);
namespace XonneX\AdventOfCode\Y2021\Solutions\Day1;
use XonneX\AdventOfCode\Core\AbstractSolution;
use function array_key_exists;
use function explode;
class Day1 extends AbstractSolution
{
public function __construct()
{
parent::__construct(2021, 1);
}
protected function partOne(string $input): string
{
$parts = explode("\n", $input);
$previousPart = null;
$incrementCount = 0;
foreach ($parts as $part) {
$part = (int) $part;
if ($previousPart === null) {
$previousPart = $part;
continue;
}
if ($part > $previousPart) {
$incrementCount++;
}
$previousPart = $part;
}
return (string) $incrementCount;
}
protected function partTwo(string $input): string
{
$parts = explode("\n", $input);
$totals = [];
foreach ($parts as $key => $part) {
$part = (int) $part;
if (!array_key_exists($key - 1, $parts) || !array_key_exists($key + 1, $parts)) {
continue;
}
$totals[] = (int) $parts[$key - 1] + $part + (int) $parts[$key + 1];
}
$previousTotal = null;
$incrementCount = 0;
foreach ($totals as $total) {
$total = (int) $total;
if ($previousTotal === null) {
$previousTotal = $total;
continue;
}
if ($total > $previousTotal) {
$incrementCount++;
}
$previousTotal = $total;
}
return (string) $incrementCount;
}
}