-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.1.php
116 lines (107 loc) · 2.13 KB
/
6.1.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
<?php
$input = <<<EOT
181, 47
337, 53
331, 40
137, 57
200, 96
351, 180
157, 332
113, 101
285, 55
189, 188
174, 254
339, 81
143, 61
131, 155
239, 334
357, 291
290, 89
164, 149
248, 73
311, 190
54, 217
285, 268
354, 113
318, 191
182, 230
156, 252
114, 232
159, 299
324, 280
152, 155
295, 293
194, 214
252, 345
233, 172
272, 311
230, 82
62, 160
275, 96
335, 215
185, 347
134, 272
58, 113
112, 155
220, 83
153, 244
279, 149
302, 167
185, 158
72, 91
264, 67
EOT;
$input_strings_array = explode(PHP_EOL, $input);
$coordinates = [];
$end_x = 0;
$end_y = 0;
$i = 0;
foreach($input_strings_array as $input_string) {
$this_coordinates = explode(", ", $input_string);
$end_x = $this_coordinates[0] > $end_x ? $this_coordinates[0] : $end_x;
$end_y = $this_coordinates[1] > $end_y ? $this_coordinates[1] : $end_y;
$coordinates[$this_coordinates[0]][$this_coordinates[1]] = 0;
}
$x1 = 0;
while ($x1 <= $end_x) {
$y1 = 0;
while ($y1 <= $end_y) {
$closest = [
'distance' => null,
'point' => [
'x' => null,
'y' => null,
],
'multiple' => null,
];
foreach ($coordinates as $x2 => $point_array) {
foreach ($point_array as $y2 => $point) {
$distance = abs($x2 - $x1) + abs($y2 - $y1);
if ($distance < $closest['distance'] || $closest['distance'] === null) {
$closest['distance'] = $distance;
$closest['point'] = [
'x' => $x2,
'y' => $y2,
];
$closest['multiple'] = false;
} elseif ($distance === $closest['distance']) {
$closest['multiple'] = true;
}
}
}
if ($x1 === 0 || $y1 === 0 || $x1 === $end_x || $y1 === $end_y) {
$coordinates[$closest['point']['x']][$closest['point']['y']] = null;
} elseif ($coordinates[$closest['point']['x']][$closest['point']['y']] !== null && $closest['multiple'] !== true) {
$coordinates[$closest['point']['x']][$closest['point']['y']]++;
}
$y1++;
}
$x1++;
}
$largest_area = null;
foreach ($coordinates as $x) {
foreach ($x as $y) {
$largest_area = $y > $largest_area || $largest_area === null ? $y : $largest_area;
}
}
echo $largest_area;