Xah Talk Show 2025-12-20 Ep729 Wolfram Language, Advent of Code 2025, Day 4

Video Summary (Generated by AI, Edited by Human.)

MorphologicalComponents 2025-12-20 340d2
MorphologicalComponents 2025-12-20 340d2
xah talk show ep729 35f10
xah talk show ep729 35f10
xah talk show ep729 364f9
xah talk show ep729 364f9
..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.
..xx.xx@x.
x@@.@.@.@@
@@@@@.x.@@
@.@@@@..@.
x@.@@@@.@x
.@@@@@@@.@
.@.@.@.@@@
x.@@@.@@@@
.@@@@@@@@.
x.x.@@@.x.

xx = {
{1,2,3},
{4,5,6},
{7,8,9}
};
ArrayPad[xx, 1]
(*
{
{0, 0, 0, 0, 0},
{0, 1, 2, 3, 0},
{0, 4, 5, 6, 0},
{0, 7, 8, 9, 0},
{0, 0, 0, 0, 0}}
*)
(* example of apply operator to list element. like auto map . *)
{ x + {-1,0,+1}, y + {-1,0,+1} }
(* {{-1 + x, x, 1 + x}, {-1 + y, y, 1 + y}} *)

(* s------------------------------ *)

(* generate the indexes of the neighboring cells, include itself *)
Tuples[ { x + {-1,0,+1}, y + {-1,0,+1} }]
(*
{{-1 + x, -1 + y},
{-1 + x, y},
{-1 + x, 1 + y},
{x, -1 + y},
{x, y},
{x, 1 + y},
{1 + x, -1 + y},
{1 + x, y},
{1 + x, 1 + y}}
 *)

(* s------------------------------ *)

(* generate the indexes of the neighboring cells,
but not include itself *)
DeleteCases[Tuples[ { x + {-1,0,+1}, y + {-1,0,+1} }], {x,y} ]
(*
{{-1 + x, -1 + y},
{-1 + x, y},
{-1 + x, 1 + y},
{x, -1 + y},
{x, 1 + y},
{1 + x, -1 + y},
{1 + x, y},
{1 + x, 1 + y}}
 *)
xinput =
"..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@."

(*
we can represent this as a 2d matrix.
each cell is either on,
represented by 1 or @ , or off as 0 or dot.
the problem is, for each cell, check all its neighors,
including diagonal neighors.
If a cell has 0 to 3 neighbors that are on, then, we consider that cell as special.

The problem asks, how many special cells are there.
 *)

xx = {
{1,2,3},
{4,5,6},
{7,8,9}
};
Part[xx, 2, 2]
(* 5 *)
xinput =
"..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.";
(* turn it into a matrix of 0 and 1 *)
xmatrix = ToExpression /@ Characters /@ (StringSplit @ StringReplace[xinput, {"." -> "0", "@" -> "1"}])

(* s------------------------------ *)
Clear[getNeighbors]
getNeighbors::usage = "getNeighbors[matrix2d, row, col]
returns all the neighbors of a given cell of 2d matrix.
For example, If matrix is
{
{1,2,3},
{4,5,6},
{7,8,9}
}
then,
cell 5 returns 1 2 3 4 7 8 9.
cell 6 returns 2 3 5 8 9."

(* todo.
each time getNeighbors is called, entire matrix is created.
this is not efficient. *)

getNeighbors[xmatrix_, xrow_, xcol_] :=
Block[{xpadded=ArrayPad[xmatrix, 1] },
Map[ Function[x, Part[xpadded, First @ x +1, Last @ x +1 ]  ] ,
DeleteCases[
Tuples[ { xrow + {-1,0,+1}, xcol + {-1,0,+1} }],
{xrow,xcol} ]]
]

(* s------------------------------ *)

Dimensions @ xmatrix

Tuples[{Range @ 10,Range @ 10}]
(* {{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {1, 9}, {1, 10}, {2, 1}, {2, 2}, {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 7}, {2, 8}, {2, 9}, {2, 10}, {3, 1}, {3, 2}, {3, 3}, {3, 4}, {3, 5}, {3, 6}, {3, 7}, {3, 8}, {3, 9}, {3, 10}, {4, 1}, {4, 2}, {4, 3}, {4, 4}, {4, 5}, {4, 6}, {4, 7}, {4, 8}, {4, 9}, {4, 10}, {5, 1}, {5, 2}, {5, 3}, {5, 4}, {5, 5}, {5, 6}, {5, 7}, {5, 8}, {5, 9}, {5, 10}, {6, 1}, {6, 2}, {6, 3}, {6, 4}, {6, 5}, {6, 6}, {6, 7}, {6, 8}, {6, 9}, {6, 10}, {7, 1}, {7, 2}, {7, 3}, {7, 4}, {7, 5}, {7, 6}, {7, 7}, {7, 8}, {7, 9}, {7, 10}, {8, 1}, {8, 2}, {8, 3}, {8, 4}, {8, 5}, {8, 6}, {8, 7}, {8, 8}, {8, 9}, {8, 10}, {9, 1}, {9, 2}, {9, 3}, {9, 4}, {9, 5}, {9, 6}, {9, 7}, {9, 8}, {9, 9}, {9, 10}, {10, 1}, {10, 2}, {10, 3}, {10, 4}, {10, 5}, {10, 6}, {10, 7}, {10, 8}, {10, 9}, {10, 10}} *)

getNeighbors[xmatrix, 9, 9]

Function[x, If[ Total[x] < 4 , 1, 0]] /@
Map[
Function[x, getNeighbors[xmatrix, First @ x, Last @ x] ],
 Tuples[{Range @ 10,Range @ 10}]]

Advent of Code 2025