Xah Talk Show 2026-01-13 Ep744 Wolfram Language, Advent of Code 2025, Day 7, part 2
Analysis of the problem
you got a particle at the top, it flows down, each time it encounters a splitter “^”, it may move to the left or right.
the question asks, how many total possibilities in the end.
so, in each instance of time, there is may be n particles, and each has a position.
now lets focus on a single particle. when it runs into a splitter, it may go left or right. so that means, it adds 2 possibilities to the existing number of possibilities
- so, we begin with a state: numberOfPossibilities = 0. in each flow of a row, this may increase, and always by 2 per collision.
- then we need another state: number of particles and their positions. e.g. xstate = {8}
- the algo, is just write a function, and loop it thru the rows, and update the states. the solution is numberOfPossibilities.
.......S....... ............... .......^....... ............... ......^.^...... ............... .....^.^.^..... ............... ....^.^...^.... ............... ...^.^...^.^... ............... ..^...^.....^.. ............... .^.^.^.^.^...^. ...............
......|.|...... ......^.^......
......|.|...... .....|^|^|.....
ok, now i think, the question is about how many total possible path from top to bottom.
ok, we try this. when a particle encounter a splitter, increase possible path by 2, but when there is a particle merge, decrease by 1.
solution
xinput = ".......S....... ............... .......^....... ............... ......^.^...... ............... .....^.^.^..... ............... ....^.^...^.... ............... ...^.^...^.^... ............... ..^...^.....^.. ............... .^.^.^.^.^...^. ..............."; xmatrix = ReplaceAll[ Map[ Characters , StringSplit[xinput, "\n"]] , { "." -> 0 , "^" -> 1}]; xtotalMultiverse = 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]]}, If[Length@xcollideList === 0, xbeamState, With[{xnewBeamState = Complement[ Union[xbeamState, Flatten @ Map[Function[xpos, xpos + {-1, 1}], xcollideList ]] , xcollideList] }, xtotalMultiverse = xtotalMultiverse + Length@xnewBeamState; xnewBeamState ] ]]]; Length @ Fold[ fnewState , Flatten @ Position[ First @ xmatrix, "S" ] , Rest @ xmatrix ] (* 9 *) xtotalMultiverse (* 40 *)
xinput = ".......S....... ............... .......^....... ............... ......^.^...... ............... .....^.^.^..... ............... ....^.^...^.... ............... ...^.^...^.^... ............... ..^...^.....^.. ............... .^.^.^.^.^...^. ..............."; xmatrix = ReplaceAll[ Map[ Characters , StringSplit[xinput, "\n"]] , { "." -> 0 , "^" -> 1}]; xtotalPaths = 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]]}, If[Length@xcollideList === 0, xbeamState, With[{xnewBeamState = Complement[ Union[xbeamState, Flatten @ Map[Function[xpos, xpos + {-1, 1}], xcollideList ]] , xcollideList] }, xtotalPaths = xtotalPaths + Length @ xnewBeamState; xnewBeamState ] ]]]; Length @ Fold[ fnewState , Flatten @ Position[ First @ xmatrix, "S" ] , Rest @ xmatrix ] (* 9 *) xtotalPaths (* 40 *)
incorrect solution when run on personal input