-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.php
198 lines (163 loc) · 5.85 KB
/
10.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
$input = file("inputs/10.txt");
//$input = file("inputs/10-test.txt");
$loopLength = 0;
$map = [];
$loopMap = array_fill(0, count($input), array_fill(0, strlen(trim($input[0])), -1));
// Find Start and build map
$sLine = -1;
$sChar = -1;
for($index = 0; $index < count($input); $index++){
$line = $input[$index];
$pipes = str_split(trim($line));
for($pipeIndex = 0; $pipeIndex < count($pipes); $pipeIndex++){
if($pipes[$pipeIndex] == "S"){
$sLine = $index;
$sChar = $pipeIndex;
}
if($pipes[$pipeIndex] == "."){
$loopMap[$index][$pipeIndex] = 0;
}
}
$map[] = $pipes;
}
$loopMap[$sLine][$sChar] = 1;
//var_dump($sLine);
//var_dump($sChar);
// var_dump($map);
// | is a vertical pipe connecting north and south.
// - is a horizontal pipe connecting east and west.
// L is a 90-degree bend connecting north and east.
// J is a 90-degree bend connecting north and west.
// 7 is a 90-degree bend connecting south and west.
// F is a 90-degree bend connecting south and east.
// . is ground; there is no pipe in this tile.
// S is the starting position of the animal; there is a pipe on this tile, but your sketch doesn't show what shape the pipe has.
// Follow pipe
$prevLine = $sLine;
$prevChar = $sChar;
$currentLine = $sLine;
$currentChar = $sChar;
// find first pipe
if($currentLine-1 > 0 && in_array($map[$currentLine-1][$currentChar], ["|", "F", "7"])){
$currentLine = $currentLine-1;
}else if(in_array($map[$currentLine+1][$currentChar], ["|", "J", "L"])){
$currentLine = $currentLine+1;
}else if(!in_array($map[$currentLine][max($currentChar-1, 0)], [".", "|"])){
$currentChar = max($currentChar-1, 0);
}else if(!in_array($map[$currentLine][min($currentChar+1, count($map[$currentLine]))], [".", "|"])){
$currentChar = min($currentChar+1, count($map[$currentLine]));
}
//var_dump($currentLine);
//var_dump($currentChar);
//var_dump($map[$currentLine][$currentChar]);
$lastDirectionLine = $currentLine - $prevLine;
$lastDirectionChar = $currentChar - $prevChar;
do{
$pipeType = $map[$currentLine][$currentChar];
//$loopMap[$currentLine][$currentChar] = $loopLength+2;
$loopMap[$currentLine][$currentChar] = 1;
//var_dump($pipeType);
$directionLine = 0;
$directionChar = 0;
if($pipeType == "|"){
if($lastDirectionLine == 1){
$directionLine = 1;
}else{
$directionLine = -1;
}
}else if($pipeType == "-"){
if($lastDirectionChar == 1){
$directionChar++;
}else{
$directionChar--;
}
}else if($pipeType == "L"){
// Von oben nach unten
if($lastDirectionLine == 1){
//$directionLine += 1;
$directionChar += 1;
}else{
$directionLine -= 1;
//$directionChar -= 1;
}
}else if($pipeType == "J"){
// Von oben nach unten
if($lastDirectionLine == 1){
//$directionLine += 1;
$directionChar -= 1;
}else{
$directionLine -= 1;
//$directionChar += 1;
}
}else if($pipeType == "7"){
// Von unten nach oben
if($lastDirectionLine== -1){
//$directionLine -= 1;
$directionChar -= 1;
}else{
$directionLine += 1;
//$directionChar += 1;
}
}else if($pipeType == "F"){
// Von unten nach oben
if($lastDirectionLine== -1){
//$directionLine -= 1;
$directionChar += 1;
}else{
$directionLine += 1;
// $directionChar -= 1;
}
}else{
break;
}
// $prevLine = $currentLine;
// $prevChar = $currentChar;
$currentLine+=$directionLine;
$currentChar+=$directionChar;
$lastDirectionLine = $directionLine;
$lastDirectionChar = $directionChar;
// var_dump($currentLine);
// var_dump($currentChar);
$loopLength++;
// var_dump($currentLine != $sLine || $currentChar != $sChar);
}while($currentLine != $sLine || $currentChar != $sChar);
var_dump(($loopLength+1) / 2);
$area = 0;
for($lineIndex = 0; $lineIndex < count($loopMap); $lineIndex++){
for($pipeIndex = 0; $pipeIndex < count($loopMap[$lineIndex]); $pipeIndex++){
// Part of loop
if($loopMap[$lineIndex][$pipeIndex] == 1){
continue;
}
$insideRight = false;
$insideLeft = false;
for($a = 0; $a < $lineIndex; $a++){
$partOfLoop = $loopMap[$a][$pipeIndex] == 1;
if ($partOfLoop&& in_array($map[$a][$pipeIndex], ["-", "L", "F"])){
$insideRight = !$insideRight;
}
if ($partOfLoop && in_array($map[$a][$pipeIndex], ["-", "7", "J"])){
$insideLeft = !$insideLeft;
}
}
if($insideRight && $insideLeft){
$area++;
}
}
}
var_dump($area);
// foreach($loopMap as $line){
// foreach($line as $elem){
// echo str_pad($elem, 1, " ", STR_PAD_LEFT);
// // if($elem == 1){
// // echo 1;
// // }else if($elem == 2){
// // echo 2;
// // }else{
// // echo 0;
// // }
// }
// echo "\n";
// }
?>