-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10.php
73 lines (57 loc) · 1.52 KB
/
Day10.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
<?php
declare(strict_types=1);
namespace XonneX\AdventOfCode\Y2020\Solutions\Day10;
use XonneX\AdventOfCode\Core\AbstractSolution;
use function array_key_exists;
use function array_pop;
use function explode;
use function ksort;
use function max;
use function next;
use function sort;
class Day10 extends AbstractSolution
{
public function __construct()
{
parent::__construct(2020, 10);
}
protected function partOne(string $input): string
{
$lines = explode("\n", $input);
sort($lines);
$counts = [
1 => 0,
2 => 0,
3 => 0,
];
$previousValue = 0;
foreach ($lines as $value) {
$counts[$value - $previousValue]++;
$previousValue = $value;
}
$counts[3]++;
return (string) ($counts[3] * $counts[1]);
}
protected function partTwo(string $input): string
{
$lines = explode("\n", $input);
$lines[] = 0;
$lines[] = max($lines);
$counters = [];
foreach ($lines as $line) {
$counters[(int) $line] = 0;
}
$counters[0] = 1;
ksort($counters);
$count = 1;
do {
$key = key($counters);
for ($i = 1; $i < 4; $i++) {
if (array_key_exists($key + $i, $counters)) {
$counters[$key + $i] += $count;
}
}
} while ($count = next($counters));
return (string) array_pop($counters);
}
}