Today you are looking at a strange metal country with lots of pipes. How did I get there? Oh, with the glider - I had to read it again while writing this post. After quickly sketching the positions of all the pipes, you get this
1..F7.
2.FJ|.
3SJ.L7
4|F--J
5LJ...
And also have a legend of what each of the symbols mean. For easier understanding, I made a much simpler map that contains only empty ground and starting position.
1. . .
2. S .
3. . .
Lets say starting position is at [0, 0] so the other positions will looks like this
1[-1,-1] [-1,0] [-1,1]
2[0,-1] Â [0,0] Â [0,1]
3[1,-1] Â [1,0] Â [1,1]
This tells me how the pipes are connected to the each other
1'|' => [[0, -1], [0, 1]], // up and down (N/S)
2'-' => [[-1, 0], [1, 0]], // left and right (W/E)
3'L' => [[-1, 0], [0, 1]], // 90 degree left (N/E)
4'J' => [[-1, 0], [0, -1]], // 90 degree right (N/W)
5'7' => [[1, 0], [0, -1]], // 90 degree left (S/W)
6'F' => [[1, 0], [0, 1]], // 90 degree right (S/E)
7'.' => [[0, 0]], // ground (no direction from here)
8'S' => [[0, 0]], // starting positifion
And to find the coordinates of my starting position, I used
1foreach ($map as $index => $row) {
2Â Â if (in_array('S', $row)) {
3Â Â Â Â $start = [$index, array_search('S', $row)]; // [2, 0] for test input
4Â Â }
5}
It is at [2,0] so only possible directions are N, S, and E
At this point I still didn’t know what to do with it :D I just knew that I had to find a loop and find how many steps it takes to get to the farthest point from the starting position.
1..45.
2.236.
301.78
414567
523...
For test input it is 8.
I was able to find the start point and now I had to find the next one. My first attempt was to check all the destinations around my current position. Yes, this worked for the S pipe, but not for the special pipes.
I created another function to get the destination for a pipe type based on its location on the map.
1$getPipe = function($x, $y) use ($map, $pipes): array {
2 $pipe = $map[$x][$y];
3 if (!isset($pipes[$pipe]) || $pipe == 'S') {
4 return $pipes;
5 }
6
7 return [$pipes[$pipe]];
8};
But not all the pipes around my current position are connected to it. When I find a possible next position, I check if it is connected back by looking at the shape of the pipe at that position and where it can connect.
1$areConnected = function (array $start, array $destination) use ($map, $pipes): bool {
2 [$x,$y] = $start;
3 [$dx, $dy] = $destination;
4 $pipe = $map[$dx][$dy];
5
6 // connected to each pipe when tere is connection from it
7 if ($pipe == 'S') {
8 return true;
9 }
10
11 foreach($pipes[$pipe] as $direction) {
12 [$px, $py] = $direction;
13 [$px, $py] = [$dx + $px, $dy + $py];
14
15 if ($px == $x && $py == $y) {
16 return true;
17 }
18 }
19 return false;
20};
I have tested this function manually and I get the following responses. They looked fine to me.
12 Possible connections for [2,0] S
2Possible connection 0: 3 0 |
3Possible connection 1: 2 1 J
4
5. . .
6
72 Possible connections for [1,3] |
8Possible connection 0: 0 3 7
9Possible connection 1: 2 3 L
10
112 Possible connections for [2,3] L
12Possible connection 0: 1 3 |
13Possible connection 1: 2 4 7
14
152 Possible connections for [2,4] 7
16Possible connection 0: 3 4 J
17Possible connection 1: 2 3 L
18
192 Possible connections for [3,4] J
20Possible connection 0: 2 4 7
21Possible connection 1: 3 3 -
22
232 Possible connections for [3,3] -
24Possible connection 0: 3 2 -
25Possible connection 1: 3 4 J
26
272 Possible connections for [3,1] F
28Possible connection 0: 4 1 J
29Possible connection 1: 3 2 -
30
312 Possible connections for [4,1] J
32Possible connection 0: 3 1 F
33Possible connection 1: 4 0 L
34
352 Possible connections for [4,0] L
36Possible connection 0: 3 0 |
37Possible connection 1: 4 1 J
38
392 Possible connections for [3,0] |
40Possible connection 0: 2 0 S
41Possible connection 1: 4 0 L
So it was time to put it all together.
1function findNext($x, $y, $map, $pipes, $next): array
2{
3
4Â Â $areConnected = function (array $start, array $destination) use ($map, $pipes): bool {
5Â Â Â Â [$x,$y] = $start;
6Â Â Â Â [$dx, $dy] = $destination;
7Â Â Â Â $pipe = $map[$dx][$dy];
8
9Â Â Â Â // / connected to each pipe when tere is connection from it
10Â Â Â Â if ($pipe == 'S') {
11Â Â Â Â Â Â return true;
12Â Â Â Â }
13
14Â Â Â Â foreach($pipes[$pipe] as $direction) {
15Â Â Â Â Â Â [$px, $py] = $direction;
16Â Â Â Â Â Â [$px, $py] = [$dx + $px, $dy + $py];
17
18Â Â Â Â Â Â if ($px == $x && $py == $y) {
19Â Â Â Â Â Â Â Â return true;
20Â Â Â Â Â Â }
21Â Â Â Â }
22
23Â Â Â Â return false;
24
25Â Â };
26
27Â Â $getPipe = function($x, $y) use ($map, $pipes): array {
28Â Â Â Â $pipe = $map[$x][$y];
29
30Â Â Â Â if (!isset($pipes[$pipe]) || $pipe == 'S') {
31Â Â Â Â Â Â return $pipes;
32Â Â Â Â }
33
34Â Â Â Â return [$pipes[$pipe]];
35Â Â };
36
37Â Â foreach ($getPipe($x,$y) as $index => $pipe)
38Â Â {
39Â Â Â Â foreach ($pipe as $dIndex => $direction) {
40Â Â Â Â Â Â [$dx, $dy] = $direction;
41Â Â Â Â Â Â [$dx, $dy] = [$x + $dx, $y + $dy];
42
43Â Â Â Â Â Â // this is the same location
44Â Â Â Â Â Â if ($x == $dx && $y == $dy) {
45Â Â Â Â Â Â Â Â continue;
46Â Â Â Â Â Â }
47
48Â Â Â Â Â Â // skip if location not exists
49Â Â Â Â Â Â if (!isset($map[$dx][$dy])) {
50Â Â Â Â Â Â Â continue;
51Â Â Â Â Â Â }
52
53Â Â Â Â Â Â // skip if ground
54Â Â Â Â Â Â if ($map[$dx][$dy] == '.') {
55Â Â Â Â Â Â Â Â continue;
56Â Â Â Â Â Â }
57
58Â Â Â Â Â Â // check if both points are connected
59Â Â Â Â Â Â if (!$areConnected([$x, $y], [$dx, $dy])) {
60Â Â Â Â Â Â Â Â continue;
61Â Â Â Â Â Â }
62
63Â Â Â Â Â Â $possibleNext = [
64Â Â Â Â Â Â Â Â $dx,
65Â Â Â Â Â Â Â Â $dy,
66Â Â Â Â Â Â ];
67
68Â Â Â Â Â Â if (array_search($possibleNext, $next) !== false) {
69Â Â Â Â Â Â Â Â continue;
70Â Â Â Â Â Â } else {
71Â Â Â Â Â Â Â Â $next[] = $possibleNext;
72Â Â Â Â Â Â Â Â $possibleNext = [];
73Â Â Â Â Â Â }
74Â Â Â Â }
75Â Â }
76
77Â Â return $next;
78}
Now I finally had a working function which returns me each location to which pipe is connected. Every time there are 2, only exception is start pipe as I mentioned above.
I still had one piece missing. How to count the farthest path. For this I created another array where I put every possible next location that my function returns. (possibleNext locations are pipes that are connected to my current location)
In the next step I compared these (from findNext function) against this connections array and removed location if it was checked before (is already in connections).
When hasNext was zero, I knew that I had checked every connected location in the loop.
1while($hasNext) {
2  foreach($next as  $index => $possibleNext) {
3Â Â Â Â [$x, $y] = [$possibleNext[0], $possibleNext[1]];
4Â Â Â Â echo 'Checking [' . $x . ',' . $y . ']' . PHP_EOL;
5Â Â Â Â $next = findNext($x, $y, $map, $pipes, $next);
6
7Â Â Â Â foreach($next as $nIndex => $nValue) {
8Â Â Â Â Â Â if (array_search([$nValue[0], $nValue[1]], $connections) !== false) {
9Â Â Â Â Â Â Â Â unset($next[$nIndex]);
10Â Â Â Â Â Â } else {
11Â Â Â Â Â Â Â Â $connections[] = [$nValue[0], $nValue[1]];
12Â Â Â Â Â Â }
13Â Â Â Â }
14
15Â Â Â Â $hasNext = count($next) > 0;
16Â Â }
17}
Next I just counted how many entires I had in the connections array and divided by 2 to get the furthest point.
🥳 Tada! Wow, that was hard. Only part 1 for me today, but I’m really glad I did it.