-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.php
53 lines (45 loc) · 1.25 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
<?php
include(__DIR__."/_loader.php");
$input = InputHelper::loadFile("datasets/day4.txt");
// Part 1
$total = 0;
foreach ($input as $line) {
$pairs = explode(',', $line);
$ranges = [];
foreach ($pairs as $pair) {
$tmp = explode('-', $pair);
$ranges[] = range($tmp[0], $tmp[1]);
}
foreach ($ranges as $k => $range) {
foreach ($ranges as $k2 => $range2) {
if ($k == $k2) continue;
$common = array_intersect($range, $range2);
if ($common == $range || $common == $range2) {
$total++;
break 2;
}
}
}
}
echo "Answer #1: ".$total.PHP_EOL;
// Part 2
$total = 0;
foreach ($input as $line) {
$pairs = explode(',', $line);
$ranges = [];
foreach ($pairs as $pair) {
$tmp = explode('-', $pair);
$ranges[] = range($tmp[0], $tmp[1]);
}
foreach ($ranges as $k => $range) {
foreach ($ranges as $k2 => $range2) {
if ($k == $k2) continue;
$common = array_intersect($range, $range2);
if (! empty($common)) {
$total++;
break 2;
}
}
}
}
echo "Answer #2: ".$total.PHP_EOL;