-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day1.php
63 lines (54 loc) · 1.71 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
<?php
declare(strict_types=1);
namespace App;
use App\Contracts\DayBehaviour;
class Day1 extends DayBehaviour
{
/**
* Find the two entries that sum to 2020; what do you get if you multiply them together?
*
* @return int|null
*/
public function solvePart1(): ?int
{
$total = 2020;
$list = array_map('intval', $this->input);
foreach ($list as $num1) {
$num2 = $total - $num1;
if ($total === $num1 + $num2 && in_array($num2, $list, true)) {
return $num1 * $num2;
}
}
return null;
}
/**
* find the 3 numbers that sum to 2020.
*
* @return int|null
*/
public function solvePart2(): ?int
{
$total = 2020;
$list = array_map('intval', $this->input);
sort($list);
// 3 numbers combined must be 2020, therefore any numbers > 2020 - 2 smallest combined = 1688
// list goes from 200 to 89
$biggestPossibleNum = $total - ($list[0] + $list[1]);
$list = array_filter($list, static fn ($i) => $i < $biggestPossibleNum);
foreach ($list as $num1) {
// subtract num1 from total, we are looking for 2 numbers that add up to that
$numTarget = $total - $num1;
foreach ($list as $num2) {
if ($num2 === $num1) { // skip the same number
continue;
}
$num3 = $numTarget - $num2;
$sumTotal = $num1 + $num2 + $num3;
if ($total === $sumTotal && in_array($num3, $list, true)) {
return $num1 * $num2 * $num3;
}
}
}
return null;
}
}