Repository navigation
Expand file tree
/
Copy pathday23.php
More file actions
81 lines (77 loc) · 2.97 KB
/
Copy pathday23.php
File metadata and controls
81 lines (77 loc) · 2.97 KB
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
<?php
$input = file_get_contents('inputs/day23.txt');
class Day23 {
public array $connections = [];
public array $threeConnections = [];
public string $biggestLan;
public int $biggestLanSize = 0;
public array $tried;
public function __construct(string $input) {
foreach (explode("\n", $input) as $line) {
[$l, $r] = explode('-', trim($line));
$this->connections[$l][] = $r;
$this->connections[$r][] = $l;
}
}
public function findThreeConnections(): void {
$this->tried = [];
foreach ($this->connections as $key => $computer) {
$tried[$key] = true;
foreach ($computer as $c) {
if (isset($tried[$c])) {
continue;
}
foreach($computer as $c2) {
if (isset($tried[$c2])) {
continue;
}
if (in_array($c2, $this->connections[$c])) {
if (str_starts_with($key, 't') || str_starts_with($c, 't') || str_starts_with($c2, 't')) {
$three = [$key, $c, $c2];
sort($three);
$this->threeConnections[implode('-', $three)] = true;
}
}
}
}
}
}
public function findAllConnections($computer): void {
$allIntersections = [];
foreach ($this->connections[$computer] as $c) {
$tempIntersections = array_intersect($this->connections[$computer], $this->connections[$c]);
$tempIntersections[] = $c;
$tempIntersections[] = $computer;
sort($tempIntersections);
if (isset($allIntersections[implode('-', $tempIntersections)])) {
$allIntersections[implode('-', $tempIntersections)]++;
continue;
}
$allIntersections[implode('-', $tempIntersections)] = 1;
}
if (max($allIntersections) < $this->biggestLanSize) {
return;
}
arsort($allIntersections);
foreach ($allIntersections as $key => $value) {
if ($value < $this->biggestLanSize) {
continue;
}
if ($value == count(explode('-', $key)) - 1) {
$this->biggestLanSize = $value;
$this->biggestLan = $key;
}
}
}
public function findBiggestLan(): void {
foreach ($this->connections as $key => $computer) {
$this->findAllConnections($key);
}
$this->biggestLan = str_replace('-', ',', $this->biggestLan);
}
}
$day23 = new Day23($input);
$day23->findThreeConnections();
echo "<p>The answer for part 1 is: " . count($day23->threeConnections) . "</p>";
$day23->findBiggestLan();
echo "<p>The answer for part 2 is: " . $day23->biggestLan . "</p>";