Xah Talk Show 2025-12-23 Ep732 Wolfram Language, Advent of Code 2025, Day 4, part 2. take 2

xah talk show ep732 14e8f
xah talk show ep732 14e8f

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

This video discusses the solution to Advent of Code 2025, Day 4, Part 2 (0:12). The problem involves a grid of @ signs and dots, where the goal is to iteratively remove @ signs that have fewer than four @ neighbors until no more can be removed. The final answer is the total number of @ signs removed (3:40).

The presenter, Xah Lee, codes the solution in Wolfram Language (Mathematica), emphasizing a pure functional programming approach with no variable mutation, for loops, or object-oriented concepts (39:50). He breaks down the problem into sub-functions:

Identifying special @ signs: Finding @ signs with less than four @ neighbors (2:36).

Removing @ signs: Replacing identified @ signs with zeros in the matrix (1:08:49).

Iterative process: Repeating the removal until no more @ signs can be removed, using the FixedPoint function in Wolfram Language (13:18).

The presenter demonstrates debugging steps when the initial code doesn't work, leading to the identification of an incorrect approach to "deleting" elements from the matrix (1:08:49). The final solution for the toy example yields 43 removed @ signs (1:14:41).

xinput =
"..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.";

(* turn it into a matrix of 0 and 1 *)
xmatrix = ToExpression /@ Characters /@ (StringSplit @ StringReplace[xinput, {"." -> "0", "@" -> "1"}])

(*
{{0, 0, 1, 1, 0, 1, 1, 1, 1, 0},
{1, 1, 1, 0, 1, 0, 1, 0, 1, 1},
{1, 1, 1, 1, 1, 0, 1, 0, 1, 1},
{1, 0, 1, 1, 1, 1, 0, 0, 1, 0},
{1, 1, 0, 1, 1, 1, 1, 0, 1, 1},
{0, 1, 1, 1, 1, 1, 1, 1, 0, 1},
{0, 1, 0, 1, 0, 1, 0, 1, 1, 1},
{1, 0, 1, 1, 1, 0, 1, 1, 1, 1},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 0},
{1, 0, 1, 0, 1, 1, 1, 0, 1, 0}}
 *)

(* 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,
getNeighbors[matrix2d, 1, 1] returns 2 4 5.
getNeighbors[matrix2d, 2, 2] returns 1 2 3 4 7 8 9.";

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

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

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

(*
algo steps:
Input is a matrix.
Go thru the matrix, remove some cells. Count number of cells removed.
Repeat, untill no cell can be removed.
Get total number of cells removed.
*)

(*
plan.
write a function ff.
ff[{inputMatrix, currentCountOfRemovedCells}]
it will return a list
{newMatrix, totalRemovedCellsCount}
then, the solution is
FixedPoint[ff, {inputMatrix,0}]
*)

Clear[ff]

ff[{xmatrix_, removeCount_}] :=
 Block[{xAtSignPosList, xToRemovePosList, resultMatrix, xRemoveCount},
   xAtSignPosList = Position[xmatrix, 1, {2}];
  xToRemovePosList =
   Select[xAtSignPosList,
    Function[x,
     If[Total@getNeighbors[xmatrix, x] < 4, True, False]]];
  xRemoveCount = Length@xToRemovePosList;
  resultMatrix =
   ReplacePart[xmatrix,
    Map[Function[x, Rule[x, 0]], xToRemovePosList]];
  {resultMatrix, removeCount + xRemoveCount}]

FixedPoint[ff, {xmatrix,0}]

(* 
{{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0,
   0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 1, 0, 0, 0, 0}, {0, 0,
   0, 1, 1, 1, 1, 0, 0, 0}, {0, 0, 0, 1, 1, 1, 1, 1, 0, 0}, {0, 0, 0,
   1, 0, 1, 0, 1, 1, 0}, {0, 0, 0, 1, 1, 0, 1, 1, 1, 0}, {0, 0, 0, 1,
   1, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 1, 1, 1, 0, 0, 0}},
43}
 *)

Interesting math question. can be a coding problem. How many neighbors a middle cell of an n dimensional matrix has? (counting diagonals)

Advent of Code 2025