-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.php
91 lines (74 loc) · 1.85 KB
/
12.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
<?php
function area_and_circ(&$lines, &$visited, $x, $y) {
$queue = array(array($x, $y));
$target = $lines[$y][$x];
$dirs = array(
array(1, 0),
array(0, -1),
array(0, 1),
array(-1, 0),
);
$area = 0;
$circ = 0;
while (count($queue) > 0) {
$curr = array_shift($queue);
$cx = $curr[0];
$cy = $curr[1];
if ($cx >= 0 && $cx < strlen($lines[$cy]) && $cy >= 0 && $cy < count($lines) && $lines[$cy][$cx] == $target && !isset($visited["$cx,$cy"])) {
$visited["$cx,$cy"] = true;
$area += 1;
foreach ($dirs as $dir) {
$dx = $cx + $dir[0];
$dy = $cy + $dir[1];
if ($dx < 0 || $dy < 0 || $dy >= count($lines) || $dx >= strlen($lines[$dy]) || $lines[$dy][$dx] != $target) {
$circ += 1;
} else {
$queue[] = array($dx, $dy);
}
}
}
}
return array($area, $circ);
}
function p1() {
// Read file into lines
$file = fopen("input", "r") or die("Cannot open file");
$lines = array();
while(!feof($file)) {
$lines[] = trim(fgets($file));
}
// Calculate the sum of products
$visited = array();
$sum = 0;
for ($j = 0; $j != count($lines); $j++) {
for ($i = 0; $i != strlen($lines[$j]); $i++) {
$ca = area_and_circ($lines, $visited, $i, $j);
$sum += $ca[0] * $ca[1];
}
}
// Print
print "$sum\n";
}
function p2() {
// Read file into lines
$file = fopen("input2", "r") or die("Cannot open file");
$lines = array();
while(!feof($file)) {
$lines[] = trim(fgets($file));
}
// Calculate the sum of products
$visited = array_fill(0, count($lines), array_fill(0, strlen($lines[0]), false));
$sum = 0;
for ($i = 0; $i < count($lines); $i++) {
for ($j = 0; $j < strlen($lines[$i]); $j++) {
if (!$visited[$i][$j]) {
// TODO
}
}
}
// Print
print "$sum\n";
}
p1();
p2();
?>