Xah Talk Show 2024-05-10 Ep554 WolframLang Coding, Advent of Code 2023, Day 10

vidthumb Lj7SCIEcgk8
The pipes are arranged in a two-dimensional grid of tiles:

| 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.
Based on the acoustics of the animal's scurrying, you're confident the pipe that contains the animal is one large, continuous loop.

For example, here is a square loop of pipe:

.....
.F-7.
.|.|.
.L-J.
.....

If the animal had entered this loop in the northwest corner, the sketch
would instead look like this:

.....
.S-7.
.|.|.
.L-J.
.....

In the above diagram, the S tile is still a 90-degree F bend: you can
tell because of how the adjacent pipes connect to it.

Unfortunately, there are also many pipes that aren't connected to the loop! This sketch shows the same loop as above:

-L|F7
7S-7|
L|7||
-L-J|
L|-JF

In the above diagram, you can still figure out which pipes form the
main loop: they're the ones connected to S, pipes those pipes connect
to, pipes those pipes connect to, and so on.

Every pipe in the main loop connects to its two neighbors

(including S, which will have exactly
two pipes connecting to it, and which is assumed to connect back to
those two pipes).

Here is a sketch that contains a slightly more complex main loop:

..F7.
.FJ|.
SJ.L7
|F--J
LJ...

Here's the same example sketch with the extra, non-main-loop pipe tiles
also shown:

7-F7-
.FJ|7
SJLL7
|F--J
LJ.LJ

If you want to get out ahead of the animal, you should find the tile
in the loop that is farthest from the starting position.

Because the animal is in the pipe, it doesn't make sense
to measure this by direct distance.

Instead, you need to find the tile that would take the longest number of steps along the loop to reach from the starting point - regardless of which way around the loop the animal went.

In the first example with the square loop:

.....
.S-7.
.|.|.
.L-J.
.....

You can count the distance each tile in the loop is from the starting point like this:

.....
.012.
.1.3.
.234.
.....

In this example, the farthest point from the start is 4 steps away.

Here's the more complex loop again:

..F7.
.FJ|.
SJ.L7
|F--J
LJ...

Here are the distances for each tile on that loop:

..45.
.236.
01.78
14567
23...

Find the single giant loop starting at S. How many steps along the
loop does it take to get from the starting position to the point farthest
from the starting position?

sample input:

..F7.
.FJ|.
SJ.L7
|F--J
LJ...

solution is 8

(* algorithm.
c:/Users/xah/web/xahlee_info/talk_show/i/advent_of_code_2023_day_10_input.txt
Put the input into a matrix. that is, nested array.
each array item is a tile, and it is a single char.
Given a tile, it give 2 other matrix indexes the tile connects to.
a tile is identified by id (row index, and column index).

Find the starting tile S. find its id. push the id to a result list.
Get at all the neighbor tiles of S.
It has 4 neighbors or less (when s is on edge)
look at each one, for example, for the east neighbor, 3 possible tiles are valid,
they are, - J 7
similarly, for each direction, you have 3 possible valid chars, that makes the path valid.
but in the end, there should be 2 tiles connected to s.
then, you can pick either one of them, and begin traverse.

traverse step algo:
when you arrive at a tile,
find the 2 tile it connect to. pick the one that's new.

repeat this until u reach a tile that is s. (back to s)

 *)

toyinput = "
..F7.
.FJ|.
SJ.L7
|F--J
LJ..."

(* StringSplit[ toyinput, "\n" ] *)

(* once u got a matrix *)

(* go thru matrix, find coord of S *)
(* push s to a result path. each item is a coordinate *)
path = {}

tile["L"] := {"north", "south"}
tile["L"][{row_, col_}] := { {row -1, col } , {row , col + 1}}

tile["-"] := {"east", "west"}
tile["-"][{row_, col_}] := { {row , col +1} , {row , col -1}}

(* possible chars
- | F 7 L J

 ─ -
│ |
 ┌ f
 ┐ 7
└ L
┘ J
 *)