-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.php
More file actions
39 lines (32 loc) · 883 Bytes
/
solution.php
File metadata and controls
39 lines (32 loc) · 883 Bytes
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
<?php
$triangles = [];
while ($row = trim(fgets(STDIN))) {
$triangles[] = explode(' ', preg_replace('/\s+/', ' ', $row));
}
function possible_triangle_count(array $triangles): int
{
return array_sum(
array_map(
function ($triangle) {
return array_sum($triangle) > 2 * max($triangle);
},
$triangles
)
);
}
fwrite(STDOUT, possible_triangle_count($triangles) . PHP_EOL);
function transpose(array $input): array
{
$output = [];
for ($y = 0; $y < count($input); $y++) {
for ($x = 0; $x < count($input[$y]); $x++) {
$output[$x][$y] = $input[$y][$x];
}
}
return $output;
}
$possible = 0;
for ($i = 0; $i < count($triangles); $i += 3) {
$possible += possible_triangle_count(transpose(array_slice($triangles, $i, 3)));
}
fwrite(STDOUT, $possible . PHP_EOL);