-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6.php
207 lines (174 loc) · 4.36 KB
/
day6.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
const DUPLICATES = -1;
print "Part 1: " . part1() . "\n";
print "Part 2: " . part2() . "\n";
function part1(): int
{
$coords = getCoords();
$map = buildMap($coords);
return findLargestArea($map);
}
function part2(): int
{
$coords = getCoords();
$map = buildDistanceMap($coords);
return countPointsAboveThreshold($map, 10000);
}
function getCoords(): array
{
$coords = [];
foreach (explode("\n", file_get_contents('day6-input.txt')) as $line) {
if ($coord = Coord::createFromString($line)) {
$coords[] = $coord;
}
}
return $coords;
}
/**
* @param array|Coord[] $coords
* @return Coord
*/
function getGridExtents(array $coords): Coord
{
$maxX = $maxY = 0;
foreach ($coords as $coord) {
if ($coord->x > $maxX) {
$maxX = $coord->x;
}
if ($coord->y > $maxY) {
$maxY = $coord->y;
}
}
return Coord::createFromCoords($maxX, $maxY);
}
/**
* @param array|Coord[] $coords
* @return array
*/
function buildMap(array $coords): array
{
$extents = getGridExtents($coords);
$map = [];
for ($row = 0; $row <= $extents->y; $row++) {
for ($col = 0; $col <= $extents->x; $col++) {
$map[$row][$col] = findClosestCoord($row, $col, $coords);
}
}
return $map;
}
function findClosestCoord(int $row, int $col, array $coords): int
{
$distances = [];
$sourceCoord = Coord::createFromCoords($col, $row);
foreach ($coords as $index => $targetCoord) {
$distances[$index] = $sourceCoord->getDistance($targetCoord);
}
$mins = array_keys($distances, min($distances));
if (count($mins) > 1) {
return DUPLICATES;
}
return $mins[0];
}
function findLargestArea(array $map): int
{
// count how many there are of each index
$areas = [];
foreach ($map as $row => $cols) {
foreach ($cols as $index) {
$areas[$index]++;
}
}
// remove any with infinite area - i.e. any on the edges
$maxRow = count($map) - 1;
$maxCol = count($map[0]) - 1;
foreach ($map as $row => $cols) {
foreach ($cols as $col => $index) {
if ($row === 0 || $row === $maxRow || $col === 0 || $col === $maxCol) {
if (array_key_exists($index, $areas)) {
unset($areas[$index]);
}
}
}
}
return max($areas);
}
/**
* @param array|Coord[] $coords
* @return array
*/
function buildDistanceMap(array $coords): array
{
$extents = getGridExtents($coords);
$map = [];
for ($row = 0; $row <= $extents->y; $row++) {
for ($col = 0; $col <= $extents->x; $col++) {
$map[$row][$col] = findTotalCoordDistance($row, $col, $coords);
}
}
return $map;
}
function findTotalCoordDistance(int $row, int $col, array $coords): int
{
$sourceCoord = Coord::createFromCoords($col, $row);
$totalDistance = 0;
foreach ($coords as $coord) {
$totalDistance += $sourceCoord->getDistance($coord);
}
return $totalDistance;
}
function countPointsAboveThreshold(array $map, int $threshold): int
{
$points = 0;
foreach ($map as $row => $cols) {
foreach ($cols as $value) {
if ($value < $threshold) {
$points++;
}
}
}
return $points;
}
class Coord
{
public $x;
public $y;
/**
* @param string $string
* @return Coord|null
*/
public static function createFromString(string $string)
{
$parts = explode(',', $string);
if (count($parts) === 2 && is_numeric($parts[0]) && is_numeric($parts[1])) {
$coord = new Coord();
$coord->x = (int)$parts[0];
$coord->y = (int)$parts[1];
return $coord;
}
return null;
}
/**
* @param int $x
* @param int $y
* @return Coord
*/
public static function createFromCoords(int $x, int $y): Coord
{
$coord = new Coord();
$coord->x = $x;
$coord->y = $y;
return $coord;
}
/**
* Simple Manhattan distance between this coord and the passed coord
*
* @param Coord $coord
* @return int
*/
public function getDistance(Coord $coord): int
{
$dx = abs($coord->x - $this->x);
$dy = abs($coord->y - $this->y);
return $dx + $dy;
}
}