Xah Talk Show 2026-01-09 Ep741 Wolfram Language, Advent of Code 2025, Day 7
Video Summary (Generated by AI, Edited by Human.)
- Advent of Code 2025, Day 7, using Wolfram Language (0:05).
- The problem involves a "tachyon manifold" diagram, where a beam originating from 'S' travels downward and splits into two (left and right) upon encountering a splitter character ('^').
- Problem is to compute the total number of times the beam splits (1:13:35).
- Lee first analyzes the problem by reading the description and understanding the beam's behavior (8:10). He then devises a step-by-step algorithm (27:10).
- Input Processing: Convert the raw text input into a matrix for easier manipulation (30:05). Replace the empty spaces ('.') with 0 and splitters ('^') with 1 to simplify calculations (36:40).
- Beam State Management: Maintain a list of the current beam positions (indexes) (35:55). The initial state is the position of 'S' in the first row (41:59).
- Iteration and Comparison: Loop through each row of the matrix, comparing the current beam positions with the splitter positions in the next row (21:53).
- Splitting and Merging Logic:
- If a beam's position matches a splitter's position, that beam splits into two new positions: (original_position - 1) and (original_position + 1) (26:36, 1:00:28).
- Beam merges are handled by removing duplicate positions from the list of beam states (22:33, 1:12:12).
- Edge Cases: Lee notes that the specific puzzle input does not require handling beams going off the grid (25:24).
- Wolfram Language Implementation: demo use functions like StringReplace (37:04), Characters (31:14), Position (49:28), Intersection (57:18), Map (58:24), and Flatten (1:07:56) to implement the logic.
- Heart of the solution is the function f_new_state that takes the current beam state and a row, returning the next beam state (1:14:49).
- Final Calculation: The Fold function is used to apply f_new_state iteratively across all rows, and the final answer is the Length of the resulting beam state list (1:21:04).
- Towards the end, he debugs a minor issue related to function definition, highlighting the iterative process of coding (1:23:19).
- Two major problems that costed 30 minute each, is 2 logical errors in our algorithm. One, we forgot to keep the beams that did not collide. Two, we forgot to remove the beams that did collide.
Problem analysis
.......S....... ............... .......^....... ............... ......^.^...... ............... .....^.^.^..... ............... ....^.^...^.... ............... ...^.^...^.^... ............... ..^...^.....^.. ............... .^.^.^.^.^...^. ...............
- need to keep a counter of the count of beams.
- start with one beam.
- it may increase (split) or decrease (when it merge). but never become 0, because it takes 2 beams at least to decrease.
- you need to compare current line with next line.
- you need to keep track of positions of the beams of current line.
- you need to know positions of the splitters next line.
- in a loop, compare current line with next line. compare the positions of the beams and the positions of the spliters. if they collide, adjust that position of beam into 2 new positions, i.e. -1 and +1
algo gist
- turn the text block into a matrix.
- start with a state, which is a list of indexs (represent the beam positions).
- write a loop, that go thru the rows.
- each time, compare the beam position with the spliter position in row.
- if they collide, change that beam's position into -1 and +1. (warning. watch out for edge case of beam and splitter on the edge. but it does not occur in my puzzle input.)
- this we forgot: when they collide, remove the beam. also, when no collide, keep the beam.
Solution. code
sample test case
xinput = ".......S....... ............... .......^....... ............... ......^.^...... ............... .....^.^.^..... ............... ....^.^...^.... ............... ...^.^...^.^... ............... ..^...^.....^.. ............... .^.^.^.^.^...^. ..............."; {x1stRow, xmatrix} = Function[x, {First @ x, Rest @ x }] @ ReplaceAll[ Map[ Characters , StringSplit[xinput, "\n"]] , { "." -> 0 , "^" -> 1}] (* {0, 0, 0, 0, 0, 0, 0, S, 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, 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, 0, 1, 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, 0, 1, 0, 1, 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, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}} *) (* a list of indexs *) xbeamState = Flatten @ Position[ {0,0,0,0,0,0,0,"S",0,0,0,0,0,0,0}, "S" ] (* {8} *) xsplitterPos = Flatten @ Position[ {0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0}, 1 ] (* {6, 8, 10} *) Intersection[ {8} ,{6, 8, 10} ] (* {8} *) (* split. on a single beam state of a single beam *) Flatten @ Map[ Function[xpos, xpos+{-1,1}] , {8} ] (* {7, 9} *) (* split. on a beam state of a 2 beams *) Flatten @ Map[ Function[xpos, xpos +{-1,1}] , {8, 10} ] (* {7, 9, 9, 11} *) (* deal with merged beam. new beam state *) DeleteDuplicates[{7, 9, 9, 11}] (* {7, 9, 11} *)
xinput = ".......S....... ............... .......^....... ............... ......^.^...... ............... .....^.^.^..... ............... ....^.^...^.... ............... ...^.^...^.^... ............... ..^...^.....^.. ............... .^.^.^.^.^...^. ..............."; xmatrix = ReplaceAll[ Map[ Characters , StringSplit[xinput, "\n"]] , { "." -> 0 , "^" -> 1}]; xtotalSplit = 0; (* fnewState[xbeamState, xRow] return a new xbeamState. *) fnewState = Function[{xbeamState, xRow}, Print["xbeamState is ", xbeamState]; Print[xRow]; DeleteDuplicates@ Flatten@With[{xcollideList = Intersection[xbeamState, Flatten@Position[xRow, 1]]}, xtotalSplit = xtotalSplit + Length@xcollideList; If[Length@xcollideList === 0, xbeamState, Complement[ Union[xbeamState, Flatten @ Map[Function[xpos, xpos + {-1, 1}], xcollideList ]] , xcollideList] ]]]; Length @ Fold[ fnewState , Flatten @ Position[ First @ xmatrix, "S" ] , Rest @ xmatrix ] (* 9 *) xtotalSplit (* 21 *)
now personal input
xinput = "......................................................................S...................................................................... ............................................................................................................................................. ......................................................................^...................................................................... ............................................................................................................................................. .....................................................................^.^..................................................................... ............................................................................................................................................. ....................................................................^...^.................................................................... ............................................................................................................................................. ...................................................................^.^...^................................................................... ............................................................................................................................................. ..................................................................^.......^.................................................................. ............................................................................................................................................. .................................................................^...^.^.^.^................................................................. ............................................................................................................................................. ................................................................^.....^.^...^................................................................ ............................................................................................................................................. ...............................................................^.^.^.^...^.^.^............................................................... ............................................................................................................................................. ..............................................................^.^.^.^.^...^...^.............................................................. ............................................................................................................................................. .............................................................^.^...^.^.^.^...^.^............................................................. ............................................................................................................................................. ............................................................^...^.^.^.......^...^............................................................ ............................................................................................................................................. ...........................................................^.^.....^...^.....^.^.^........................................................... ............................................................................................................................................. ..........................................................^...^.^...^...^...^.^.^.^.......................................................... ............................................................................................................................................. .........................................................^.^.^...^.^.^.^.....^.^.^.^......................................................... ............................................................................................................................................. ........................................................^.^.....^.^.^.^.^...^.^...^.^........................................................ ............................................................................................................................................. .......................................................^.^.......^.^.^.^.....^.......^....................................................... ............................................................................................................................................. ......................................................^.^.^.^.^...^...^.^...^.........^...................................................... ............................................................................................................................................. .....................................................^.....^.^.......^.........^.^.^.^.^..................................................... ............................................................................................................................................. ....................................................^.^.^.^...^.^.^...^.....^...^...^.^.^.................................................... ............................................................................................................................................. ...................................................^...^.^.^.....^...^.^.^.^.^.^.^.......^................................................... ............................................................................................................................................. ..................................................^.^...^.^.^.^.^...^.^.....^.^.^.^.^.^.^.^.................................................. ............................................................................................................................................. .................................................^.^.^.^.^.......^.^...^.^.^.....^.^...^...^................................................. ............................................................................................................................................. ................................................^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.^.^...^...^................................................ ............................................................................................................................................. ...............................................^.^.^.^...^...^...^.^.^...^.^.^.....^.^.^...^.^............................................... ............................................................................................................................................. ..............................................^...^...^...^.....^...^...^...^...^...^.^.^.^...^.............................................. ............................................................................................................................................. .............................................^.^...^.^.^.^.^...^.^...^.^.....^.^.^.^.^.^.......^............................................. ............................................................................................................................................. ............................................^.^.^.^.^...^.^.....^...^.^.^.^.......^...^...^.^...^............................................ ............................................................................................................................................. ...........................................^.^...^.^.^...^.^.^.....^.....^...^.^.^...^...^.^.^.^.^........................................... ............................................................................................................................................. ..........................................^...^...^...^.....^.^.^.^...^.^...^.^.^.^.^.^.^.^.^.^.^.^.......................................... ............................................................................................................................................. .........................................^.^.....^.^...^...^...^.^.^.^...^.^...^.........^.^.^.....^......................................... ............................................................................................................................................. ........................................^.^.....^.^.^...^...^...^.....^...^...^.^.^.^.^.^.^.^.^.^.^.^........................................ ............................................................................................................................................. .......................................^...^.^.^.^.^...^...^.^.^.^.^.^...^...^.^...^.......^.^.^...^.^....................................... ............................................................................................................................................. ......................................^.^.^...^.^.^.....^.......^.................^...^.^.^.^.^.^.^.^.^...................................... ............................................................................................................................................. .....................................^.^.......^.....^.^...^.^.^.^.......^...^.^.^...^.^...^.^.^.^...^.^..................................... ............................................................................................................................................. ....................................^.^...^...^.^.^.^.....^...^...^.^.^.....^...^...^.^.^...^...^...^...^.................................... ............................................................................................................................................. ...................................^.^.^...^.....^...^.^.^...^...^.^...^.^.^...^.^.......^.^...^.^...^...^................................... ............................................................................................................................................. ..................................^.^.^.^.......^...^.^.^...^.^.^.^...^.^.^.....^.^.^.......^.^.^...^.^...^.................................. ............................................................................................................................................. .................................^...^...^.^.^.^...^...^...^.^.^.....^.^.^...^.^.......^.^.^.^...^.....^.^.^................................. ............................................................................................................................................. ................................^.^.^.....^.^.^.^.^.^.....^.^.^...^.^...^.....^.^.^.^.^.^.^.^.....^.^.^.^.^.^................................ ............................................................................................................................................. ...............................^...^.^.^.....^.^...^...^.......^.^.^.....^.^.^.^.^.^.^.^.^...^...^.^.....^.^.^............................... ............................................................................................................................................. ..............................^.^.^.^.^.......^.^...^...^.^.^...^...^.^.^.........^.^.^.^...^.....^...^.^.^.^.^.............................. ............................................................................................................................................. .............................^...^...^.^...^.^.^.^.........^.^.^.^.^.^...^.^...^.....^.....^.^.....^...^...^.^.^............................. ............................................................................................................................................. ............................^.......^...^.^.^.^.^...^...^.^.^.^.^.^...^.^...^.^.^.^...^.^.^.^.^.^.^...^.^.^...^.^............................ ............................................................................................................................................. ...........................^.^...^...^.^...^...^.^.^.....^.......^.^.....^.^.^.^.^.^.......^.^.^...^...^.^...^...^........................... ............................................................................................................................................. ..........................^.......^.^.^.^.^.^...^.^.^...^...^.......^...^.^.^...^.....^...^...^.....^...^.^...^.^.^.......................... ............................................................................................................................................. .........................^...^.^.^.^...^.^...^.^.^.^.^.^...^...^.^.....^.^.^.^...^.^...^.^.^.^.^.^.........^.^.^.^.^......................... ............................................................................................................................................. ........................^...^.^...^...^.^.^.^.^.^.^.^.^.^.^...^...^...^...^.......^.^.....^.^.^.^.^.^.....^...^...^.^........................ ............................................................................................................................................. .......................^...^.^...^...^.^.......^.^.^.^.^.^...^.^.^.^...^.^...^.^.^.......^.....^.^.^.^.^.^.....^.^.^.^....................... ............................................................................................................................................. ......................^.^.^.^.^.^...^...^.^.^.^...^.^.....^.....^.^.^.^...^.^.^.^.^.^...^.^...^.^.^.........^.......^.^...................... ............................................................................................................................................. .....................^.^.^.....^.....^.^.^.^.......^.^.^...^.^.^.....^...^.^.^.^...^...^.^.......^...^.^.^.^...^.^.^...^..................... ............................................................................................................................................. ....................^.....^.^.....^...^...^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^.^...^...^.^...^...^...^...^.....^...^.................... ............................................................................................................................................. ...................^.^...^...^.^.^.^.....^.^.^...^.^.....^...^...^.^.^.^...^.^...^.^.^.^.^.^.....^.^.^.^.......^...^.^.^.^................... ............................................................................................................................................. ..................^.^.......^.^...^.^...^.^.^...^.^.^.^...^.....^.^.^...^.^.^.^.^...^.^...^...^.^.^.^.^.^...^.^.^.^.^.^...^.................. ............................................................................................................................................. .................^.^.^...^.^.^.^.....^.^.^.^.^.....^.^.....^.^.^.^...^...^.^.......^.^.^.^.^...^.^.....^.^...^.^.^.^...^...^................. ............................................................................................................................................. ................^...^.^.^...^.^.....^.^.^.^.^.^.^.^...^.....^.^...^.^...^...^...^...^.^.^.....^.^.^.^.^.^.^.....^...^.^.^...^................ ............................................................................................................................................. ...............^...^.^...^.^.^...^.^...^...^.^.^.^.^.^.^.^.^.^.^.^...^.^.^...^.....^.^.......^.^...^.^...^.^.^.^.^.^.^.^.^.^.^............... ............................................................................................................................................. ..............^.^...^...^.....^...^.^...^.^...^.^.^.^.^.....^...^...^.....^.^.^...^.^...^.^...^.....^...^...^.....^...^.^.^...^.............. ............................................................................................................................................. .............^.....^.^.^.^...^.....^.^.......^.^.^.^.^.^.^.....^...^.....^.^.....^.^.^.....^...^.^...^.^.^.^...^.^.....^...^.^.^............. ............................................................................................................................................. ............^.^...^.^.......^.^.^.^.^...^.^.^.....^.^.^.^...^...^.....^...^.^...^.^.^...^.^.^.^...^...^.....^.^.^.^...^.^...^.^.^............ ............................................................................................................................................. ...........^.....^.^.^.^...^...^.^...^...^.....^.^...^...^.^.^...^.^.......^.^.^...^.^...^.^.......^.^...^.......^.^...^...^.^.^.^........... ............................................................................................................................................. ..........^.^.^.^.......^...^.^.^.^.^.^.^...^.....^.^...^.^.^.^.^.^.^.^.^...^.^.....^.^.......^.^.^.^...^.^.......^.^...^.^...^...^.......... ............................................................................................................................................. .........^.^...^.....^.^.^.^.^.^.^.^...^.^.^.^...^.^.^.^.^.^.^.^...^...^...^.^.^.^.^.^.^.^.^.^.^...^.....^.^.^.^.^.^...^.^.^...^.^.^......... ............................................................................................................................................. ........^.^.^...^.^...^.^.^.^...^.......^...^...^.^.^.^.....^.^...^.^.^.....^.^.^.^.^.^.^...^.^...^.^.^.......^...^.^...^.....^.^...^........ ............................................................................................................................................. .......^.^.^.....^.....^.....^.^.^.^.^.^...^.^...^...^...^.........^.^...^.^.^...^.^...^.^.^...^.^.........^.^.^.^.^.^...^...^.^.^.^.^....... ............................................................................................................................................. ......^...^.^...^...^.^.^...^...^.^.^.^.^.^.^.^.^.^...^...^.....^.......^.^.^.^.^.^.^.^...^.^.^...^...^.^...^.....^.......^.^.^...^.^.^...... ............................................................................................................................................. .....^.^.^.....^...^.^.............^...^.^.^.^.^.^...^...^.^.^.^.^.^.^...^...............^.......^.^.^.....^.^...^.^.^.^.......^...^.^.^..... ............................................................................................................................................. ....^...^.^.^...^...^...^.^.......^...^.^.^.^.^.^.^...^.^.^.^.^.....^.^...^.^...^.^.^.^.........^.....^.^...^...^.^.^.^.^...^.^...^...^.^.... ............................................................................................................................................. ...^...^.....^.^.^...^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.....^...^...^...^.^.^.^.^...^.^.^.^.^.^.^.^.......^.^.^.^.^.^...^.^...^.^.^...^...^... ............................................................................................................................................. ..^.^...^.^.^.^.^...^.^.^.^.^.^...^.^.^.^.^.^...^...^.....^.^.^.^.^...^.^...........^.^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.........^.^.^...^.^.. ............................................................................................................................................. .^...^.^.^.^...^.....^.^.^.^.^.^.^.^.^...^.^.^...^.^.^...^.^...^.^.^.^...^...^.....^.....^...^.^...^.^.^...^.^.....^...^.^.^.^.^.^.^.^.^.^.^. ............................................................................................................................................."; xmatrix = ReplaceAll[ Map[ Characters , StringSplit[xinput, "\n"]] , { "." -> 0 , "^" -> 1}]; xtotalSplit = 0; (* fnewState[xbeamState, xRow] return a new xbeamState. *) fnewState = Function[{xbeamState, xRow}, Print["xbeamState is ", xbeamState]; Print[xRow]; DeleteDuplicates@ Flatten@With[{xcollideList = Intersection[xbeamState, Flatten@Position[xRow, 1]]}, xtotalSplit = xtotalSplit + Length@xcollideList; If[Length@xcollideList === 0, xbeamState, Complement[ Union[xbeamState, Flatten @ Map[Function[xpos, xpos + {-1, 1}], xcollideList ]] , xcollideList] ]]]; Length @ Fold[ fnewState , Flatten @ Position[ First @ xmatrix, "S" ] , Rest @ xmatrix ] (* 87 *) xtotalSplit (* 1516 *)
- the problem we counter, is 2 logical errors in our algorithm.
- one, we forgot to keep the beams that did not collide.
- two, we forgot to remove the beams that did collide.
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, Part 2
- Xah Talk Show 2025-12-11 Ep723 Wolfram Language, Advent of Code 2025, Day 1, Part 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, Part 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)
- Xah Talk Show 2026-01-02 Ep736 Wolfram Language, Advent of Code 2025, Day 6
- Xah Talk Show 2026-01-05 Ep738 Wolfram Language, Advent of Code 2025, Day 6, Part 2
- Xah Talk Show 2026-01-09 Ep741 Wolfram Language, Advent of Code 2025, Day 7
- Xah Talk Show 2026-01-13 Ep744 Wolfram Language, Advent of Code 2025, Day 7, part 2