Xah Talk Show 2025-12-23 Ep732 Wolfram Language, Advent of Code 2025, Day 4, part 2. take 2
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
- 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)