Xah Talk Show 2024-08-03 Ep569, Advent of Code 2023, Day 11, Wolfram Language

vidthumb fybQIVQeVF8

toy input

...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
(* solution.
Turn the text into a matrix.
add extra columns and rows, for any empty column and row.
then use Position[ matrix, 1 ] to give a list of all indexes.
then use pattern matching to get all distances.
then just Total
 *)

xinput = "...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....";

xinput = ReadString["c:/Users/xah/web/xahlee_info/talk_show/i/advent_of_code_2023_day_11_input.txt"];

xmatrix = Map[ Characters , StringSplit[ xinput , "\n" ] ];

xmatrix =
ReplaceAll[
 xmatrix ,
 {emptyRow:{"."..} -> Sequence[emptyRow,emptyRow] }
];

xmatrix =
Transpose@ ReplaceAll[
 Transpose[ xmatrix ] ,
 {emptyRow:{"."..} -> Sequence[emptyRow,emptyRow] }
];

xcoordlist = Position[ xmatrix, "#" ];

xAllGalaxyPairs = Subsets[ xcoordlist, {2} ];

result =
Total@
Replace[ xAllGalaxyPairs , {{a_, b_}, {c_, d_}} :> Abs[a-c]+Abs[b-d], {1} ];

Print[ result ]

(* 
my solution
9742154
 *)
(* Advent of Code 2023, Day 11, part 1.
Wolfram Language short solution *)

xinput = "...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....";

(* xinput = ReadString["c:/Users/xah/web/xahlee_info/talk_show/i/advent_of_code_2023_day_11_input.txt"]; *)

xinput // StringSplit //
( Characters /@ # &) //
( # /. {r:{"."..} -> Sequence[r,r] } &) //
(Transpose@ (Transpose@# /. {r:{"."..} -> Sequence[r,r] }) &) //
Position[ #, "#" ] & //
Subsets[ #, {2} ] & //
( # /. {{a_, b_}, {c_, d_}} :> Abs[a-c]+Abs[b-d]) & //
Total //
Print

(*
my solution
9742154
 *)
xts 2024-08-03 F47kz
xts 2024-08-03 F47kz