-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.php
executable file
·30 lines (29 loc) · 1.27 KB
/
solution.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
#!/usr/bin/env php
<?php
include("common.php");
$lines = read_input();
$x=[];$y=[];
foreach($lines as $line) sscanf($line, "%d, %d", $x[], $y[]);
$maxx = Amax($x); $maxy = Amax($y); $n=count($x); $coords = range(0,$n-1);
$d=[0=>[]]; $g=[0=>[]]; $areas=Azeros($n);
foreach(range(0,$maxx) as $i){
foreach(range(0,$maxy) as $j){
@$g[$i][$j]=0;
$distances = [];
foreach($coords as $z) $distances[$z] = abs($i-$x[$z]) + abs($j-$y[$z]);
[$mk,$m] = Aminkv($distances);
@$d[$i][$j]=Asum($distances);
$mn = Ahistogram($distances)[$m];
//printf("i,j=$i,$j | m:$m | mk:$mk | mn:$mn | distances: %s\n",ve($distances));
if($mn>1)continue;
@$g[$i][$j]=$mk;
$areas[$mk]++;
}
}
$borderareas=[];
foreach(range(0,$maxx) as $i) { $borderareas[]= $g[$i][0]; $borderareas[]= $g[$i][$maxy]; }
foreach(range(0,$maxy) as $j) { $borderareas[]= $g[0][$j]; $borderareas[]= $g[$maxx][$j]; }
Aunsetkeys($areas, array_unique($borderareas));
printf("Part 1 answer: the size of the largest area that is not infinite is: %d\n", Amax($areas));
$size=0; foreach(range(0,$maxx) as $i) foreach(range(0,$maxy) as $j) if($d[$i][$j]<10000) $size++;
printf("Part 2 answer: the size of the region with sum(distances) < 10000 is: %d \n", $size);