Xah Talk Show 2025-12-21 Ep730 Wolfram Language, Advent of Code 2025, Day 4, take 2

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

This video, "Xah Talk Show Ep730 Wolfram Language, Advent of Code 2025, Day 4, take 2," is a continuation of a coding session focusing on solving an Advent of Code problem using the Wolfram Language.

Here's a breakdown of the key topics:

Improving the getNeighbors Function:


xah talk show ep730 29e6c
xah talk show ep730 29e6c

(* we can make our getNeighbors function more general
so it works on matrix of any dimension.
what you need to do, is of the following form

this is our current form
getNeighbors[xmatrix2D, i,j]

better form would be
getNeighbors[xmatrix2D, {i,j}]

so it can be generalized, because the second parameter is a list of indexes.
getNeighbors[xmatrix3D, {i,j,k}]
getNeighbors[xmatrix4D, {i,j,k,l}]
getNeighbors[xmatrixND, listOfIndexes]

  *)
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]
]]

getNeighbors[
{
{1,2,3},
{4,5,6},
{7,8,9}
}, 2, 2]
(* {1, 2, 3, 4, 6, 7, 8, 9} *)

getNeighbors[
{
{1,2,3},
{4,5,6},
{7,8,9}
}, 1, 1]
(* {2, 4, 5} *)
(* shortcut syntax *)
aaa @ bbb
(* is same as *)
aaa[bbb]

(* shortcut syntax *)
aaa /@ bbb
(* is same as *)
Map[ aaa , bbb]
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------------------------------ *)

Dimensions @ xmatrix;
(* {10, 10} *)

(* xallPossibleIndexs = Tuples @ (Range /@ Dimensions @ xmatrix) *)
(* {{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}} *)

(* all indexes where the cell is empty *)
(* xindexOfEmptyCells = Position[xmatrix, 0, {2}] *)

(* all indexes where the cell is a paper roll *)
xindexOfNoneEmptyCells = Position[xmatrix, 1, {2}]

Total @
(Function[x, If[ Total[x] < 4 , 1, 0]] /@
Map[
Function[x, getNeighbors[xmatrix, x] ],
 xindexOfNoneEmptyCells
])
(* 13 *)

Advent of Code 2025