-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_14.php
105 lines (91 loc) · 2.71 KB
/
day_14.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
<?php
$filePointer = fopen("input_14.txt", "r");
$cave = [];
$sandStartX = 500;
$sandStartY = 0;
$sand = [];
while (!feof($filePointer) && $line = trim(fgets($filePointer))) {
$rocks = explode(" -> ", $line);
foreach ($rocks as $key => $currentRock) {
$nextRock = $rocks[$key+1] ?? null;
if ($nextRock == null) {
continue;
}
list($currentRockX, $currentRockY) = explode(",", $currentRock);
list($nextRockX, $nextRockY) = explode(",", $nextRock);
if ($currentRockX === $nextRockX) {
for ($i = min($currentRockY, $nextRockY); $i <= max($currentRockY, $nextRockY); $i++) {
$cave[$currentRockX][$i] = "#";
}
}
if ($currentRockY === $nextRockY) {
for ($i = min($currentRockX, $nextRockX); $i <= max($currentRockX, $nextRockX); $i++) {
$cave[$i][$currentRockY] = "#";
}
}
}
}
list($minX, $maxX, $minY, $maxY) = getMinMaxCoordinates($cave);
// only for part 2
for ($i = 0; $i <= $sandStartX*2; $i++) {
$cave[$i][$maxY+2] = "#";
}
// only for part 2 end
$currentSandX = $sandStartX;
$currentSandY = $sandStartY+1;
$sandCount = 0;
do {
if (!isset($cave[$currentSandX][$currentSandY+1])) {
$currentSandY = $currentSandY+1;
continue;
}
if (!isset($cave[$currentSandX-1][$currentSandY+1])) {
$currentSandX = $currentSandX-1;
$currentSandY = $currentSandY+1;
continue;
}
if (!isset($cave[$currentSandX+1][$currentSandY+1])) {
$currentSandX = $currentSandX+1;
$currentSandY = $currentSandY+1;
continue;
}
$cave[$currentSandX][$currentSandY] = "o";
$sandCount++;
if ($currentSandX = $sandStartX && $currentSandY == $sandStartY) {
break;
}
//printCave($cave);
$currentSandX = $sandStartX;
$currentSandY = $sandStartY;
} while ($currentSandY <= $maxY+3);
printCave($cave);
echo "sand $sandCount \n";
function getMinMaxCoordinates($cave) {
$xValues = array_keys($cave);
sort($xValues);
$minX = $xValues[0];
$maxX = $xValues[count($xValues)-1];
$minY = 0;
$maxY = 0;
foreach ($cave as $value) {
$y = array_key_first($value);
if ($y > $maxY) {
$maxY = $y;
}
}
return [$minX, $maxX, $minY, $maxY];
}
function printCave($cave) {
list($minX, $maxX, $minY, $maxY) = getMinMaxCoordinates($cave);
echo "$minX $maxX $minY $maxY \n";
for ($y = $minY; $y <= $maxY; $y++) {
for ($x = $minX; $x <= $maxX; $x++) {
if ($x == 500 && $y == 0) {
echo "+";
continue;
}
echo $cave[$x][$y] ?? ".";
}
echo "\n";
}
}