-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.php
55 lines (49 loc) · 1.54 KB
/
day4.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
<?php
require_once './AbstractBenchmarking.php';
class Day4 extends AbstractBenchmarking
{
private array $data;
private array $testData;
private string $rawData;
private string $rawTestData;
public function __construct()
{
$this->rawData = file_get_contents('Data/day4.txt');
$this->rawTestData = file_get_contents('TestData/day4.txt');
$this->parseData(true);
$this->parseData();
}
public function parseData(bool $test = false): void
{
$rawInputData = $test ? $this->rawTestData : $this->rawData;
preg_match_all('/(\d+)/m', $rawInputData, $matches);
$lineInput = array_chunk(array_map('intval', $matches[1]), 4);
if ($test) {
$this->testData = $lineInput;
} else {
$this->data = $lineInput;
}
}
public function part1(bool $test = false): int
{
$inputData = $test ? $this->testData : $this->data;
$sum = 0;
foreach ($inputData as $pair) {
if (($pair[0] >= $pair[2] && $pair[1] <= $pair[3]) || ($pair[2] >= $pair[0] && $pair[3] <= $pair[1])) {
$sum++;
}
}
return $sum;
}
public function part2(bool $test = false): int
{
$inputData = $test ? $this->testData : $this->data;
$sum = 0;
foreach ($inputData as $pair) {
if (($pair[0] <= $pair[3] && $pair[1] >= $pair[2]) || ($pair[2] <= $pair[1] && $pair[3] >= $pair[0])) {
$sum++;
}
}
return $sum;
}
}