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:
- Introduction to Wolfram Language: The speaker introduces the Wolfram Language as a highly advanced programming language based on a term-rewriting system (0:46).
- He explains that it treats all syntax as symbolic strings that are continuously transformed until the desired result is achieved (2:20).
- He also mentions that it can be downloaded for free from wolfram.com/developer (1:11).
- Advent of Code Problem: The main task involves continuing to solve Day 4 of the Advent of Code 2025 challenge (0:30).
- The speaker specifically focuses on improving a previously written getNeighbors function that retrieves neighboring cells in a matrix (4:54).
Improving the getNeighbors Function:
- Handling Edge Cases: The speaker discusses how to properly handle cases where the desired neighbor is outside the matrix boundary.
- Initially, zeros were returned for out-of-bounds neighbors (9:48), but he decides to improve this by padding the matrix with a unique symbolic value and then removing these unique values from the result (12:47).
- This makes the function more robust and generally applicable.
- Efficiency: The speaker acknowledges that the current implementation of getNeighbors is not the most efficient for large matrices because it recreates a padded array each time it's called (24:07).
- However, he highlights the convenience of array padding in the Wolfram Language for avoiding manual boundary checks (24:42).
- Generalization: A significant part of the discussion revolves around making the getNeighbors function work for arbitrary dimensional matrices (27:07).
- The speaker suggests passing the cell's coordinates as a single list of indexes rather than separate row and column arguments to improve the function's generality and clarity (32:30, 53:41).
- Wolfram Language Syntax and Shortcuts: The video delves into Wolfram Language's unique syntax, particularly its full form (47:08) and various syntactical shortcuts (44:06).
- Examples like @ for Map (49:08) and // for applying functions are explained (49:56).
- Comparison with Other Numerical Software: The speaker briefly addresses a comment comparing Wolfram Language to GNU Octave (57:31).
- He clarifies that while GNU Octave (a MATLAB clone) is good for numerical computation and linear algebra, it's not comparable to Wolfram Language, which is a far more general and powerful symbolic computation system (1:00:08).
- Wolfram Language for Programers
- Wolfram: Download Wolfram Engine
- Emacs: Xah Wolfram Mode 📦
- Emacs: Xah Fly Keys 📦
- comparison of GNU Octave, MATLAB, and Wolfram language
(* 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
- Xah Talk Show 2025-12-08 Ep720 Wolfram Language, Advent of Code 2025, Day 1
- Xah Talk Show 2025-12-09 Ep721 Wolfram Language, Advent of Code 2025, Day 1, Problem 2
- Xah Talk Show 2025-12-11 Ep723 Wolfram Language, Advent of Code 2025, Day 1, Problem 2, take 2
- Xah Talk Show 2025-12-12 Ep724 Wolfram Language, Advent of Code 2025, Day 2
- Xah Talk Show 2025-12-15 Ep725 Wolfram Language, Advent of Code 2025, Day 2, Problem 2
- Xah Talk Show 2025-12-17 Ep726 Wolfram Language, Advent of Code 2025, Day 3 (aborted)
- Xah Talk Show 2025-12-18 Ep727 Wolfram Language, Advent of Code 2025, Day 3, take 2
- Xah Talk Show 2025-12-19 Ep728 Wolfram Language, Advent of Code 2025, Day 3, part 2 (failed)
- Xah Talk Show 2025-12-20 Ep729 Wolfram Language, Advent of Code 2025, Day 4
- Xah Talk Show 2025-12-21 Ep730 Wolfram Language, Advent of Code 2025, Day 4, take 2
- Xah Talk Show 2025-12-22 Ep731 Wolfram Language, Advent of Code 2025, Day 4, part 2. Wolfram vs SageMath
- Xah Talk Show 2025-12-23 Ep732 Wolfram Language, Advent of Code 2025, Day 4, part 2. take 2
- Xah Talk Show 2025-12-26 Ep733 Wolfram Language, Advent of Code 2025, Day 5
- Xah Talk Show 2025-12-27 Ep734 Wolfram Language, Advent of Code 2025, Day 5, part 2 (failed)