-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.php
More file actions
80 lines (61 loc) · 2.07 KB
/
solution.php
File metadata and controls
80 lines (61 loc) · 2.07 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
<?php
/** @var array[] $coordinates List of coordinate pairs [x, y]. */
$coordinates = [];
// Grid corners.
$minx = $miny = PHP_INT_MAX;
$maxx = $maxy = PHP_INT_MIN;
while ($row = trim(fgets(STDIN))) {
[$x, $y] = array_map('intval', explode(',', $row));
// Determine the size of the grid.
$minx = min($minx, $x);
$miny = min($miny, $y);
$maxx = max($maxx, $x);
$maxy = max($maxy, $y);
$coordinates[] = [$x, $y];
}
/** @var int[][] $distances Distances to closest coordinate. */
$distances = [];
/** @var int[][] $closest ID number of closest coordinate, or -1 in case of a tie. */
$closest = [];
/** @var int[][] $sums Sum of manhattan distances to all coordinates. */
$sums = [];
/** @var int[] $areas Size of the area each coordinate covers. */
$areas = array_fill(-1, count($coordinates) + 1, 0);
/** @var int[] $infinite List of ID's for coordinates with infinite region. */
$infinite = [
-1,
];
/** @var int $center Number of coordinates within a specified combined distance to all coordinates. */
$center = 0;
// Fill $distances, $closest and $sums.
for ($y = $miny; $y <= $maxy; $y++) {
for ($x = $minx; $x <= $maxx; $x++) {
$distances[$y][$x] = PHP_INT_MAX;
$area = -1;
$sums[$y][$x] = 0;
foreach ($coordinates as $id => $coordinate) {
$manhattan = abs($x - $coordinate[0]) + abs($y - $coordinate[1]);
$sums[$y][$x] += $manhattan;
if ($manhattan < $distances[$y][$x]) {
$distances[$y][$x] = $manhattan;
$area = $id;
} elseif ($manhattan === $distances[$y][$x]) {
$area = -1;
}
}
$closest[$y][$x] = $area;
$areas[$area]++;
if ($x === $minx || $x === $maxx || $y === $miny || $y === $maxy) {
$infinite[] = $area;
}
if ($sums[$y][$x] < 10000) {
$center++;
}
}
}
// Exclude ties as an area.
foreach (array_unique($infinite) as $area) {
unset($areas[$area]);
}
fwrite(STDOUT, max($areas) . PHP_EOL);
fwrite(STDOUT, $center . PHP_EOL);