Xah Talk Show 2026-01-05 Ep738 Wolfram Language, Advent of Code 2025, Day 6, Part 2

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

analysis of the problem (incorrect take)

s------------------------------
Analysis

consider this column

64
23
314
+

we want to pad it like this

640
230
314
+

and for each single digit column, starting from right, turn it into a number.
so that we have

4
431
623

s------------------------------
Algo Description.

we already turned it into a list, like this
{"+", "64", "23", "314"}

for each row, find the max number. (max length of string).
e.g. 3 because 314.

then, for each number, pad "0" to the right, by the max length.
e.g. {"+", "640", "230", "314"}

then, consider all these number, turn it into a matrix, each elementis a single digit.
{
{"6","4","0"},
{"2","3","0"},
{"3","1","4"}
}

then, do a transpose. we get
{
{6, 2, 3},
{4, 3, 1},
{0, 0, 4}
}

then, join string, and turn it into a number.
{
623,
431,
4
}

then, just apply the operator to these numbers.

Realization. our previous understanding of the problem is incorrect

actually, the given input text, the number of spaces between columns is significant.

look at the first column and the last column

123 328  51 64
 45 64  387 23
  6 98  215 314
*   +   *   + 

if you just split the string for each row of text, you lose the alignment info.

new algorithm

Characters @ "123.328.51.64"
(* {1, 2, 3, ., 3, 2, 8, ., 5, 1, ., 6, 4} *)

MapThread[f , {{a,b,c}, {1,2,3}}]
(* {f[a, 1], f[b, 2], f[c, 3]} *)
(* be careful. trailing spaces in input text are significant. If you delete them, it won't work. make sure your text editor does not auto delete trailing spaces.
 *)

(* let's change the input text by replace space to dot. that way, we don't have this unintended deletion of trailing whitespace. *)
xinput = "123.328..51.64.
.45.64..387.23.
..6.98..215.314
*...+...*...+..";
(* first, turn the text block into a matrix of string, each element is a single char. *)
xinputStringMatrix = Map[ Characters , StringSplit[ xinput, "\n"] ]

(*
{
{"1", "2", "3", ".", "3", "2", "8", ".", ".", "5", "1", ".", "6", "4", "."},
{".", "4", "5", ".", "6", "4", ".", ".", "3", "8", "7", ".", "2", "3", "."},
{".", ".", "6", ".", "9", "8", ".", ".", "2", "1", "5", ".", "3", "1", "4"},
{"*", ".", ".", ".", "+", ".", ".", ".", "*", ".", ".", ".", "+", ".", "."}}
 *)

xoperators = Select[ ReplaceAll[Last @ xinputStringMatrix, { "*" -> Times , "+" -> Plus}] , Function[x, UnsameQ["." , x] ]]
(* {Times, Plus, Times, Plus} *)

xmatrix = StringJoin /@ Transpose @ Most @ xinputStringMatrix

(* {"1..", "24.", "356", "...", "369", "248", "8..", "...", ".32", "581", "175", "...", "623", "431", "..4"} *)

xsplitMatrix =
SplitBy[xmatrix,
Function[x, StringContainsQ[x, RegularExpression["[0-9]"]]]
]
(*
{
{"1..", "24.", "356"}, {"..."},
{"369", "248", "8.."}, {"..."},
{".32", "581", "175"}, {"..."},
{"623", "431", "..4"}}  *)

xemptyRowRemovedMatrix = Select[ xsplitMatrix, Function[x, Length @ x > 1]]
(* {{"1..", "24.", "356"}, {"369", "248", "8.."}, {".32", "581", "175"}, {"623", "431", "..4"}}  *)

xnumberMatrix = Map[ Function[xrow, ToExpression @ Map[ Function[xstr, StringReplace[xstr, "." -> "" ]] , xrow]] , xemptyRowRemovedMatrix]
(* {{1, 24, 356}, {369, 248, 8}, {32, 581, 175}, {623, 431, 4}} *)

Total @ MapThread[ Function[{xop, xnumList}, Apply[xop, xnumList]] , {xoperators, xnumberMatrix}]
(* 3263827 *)

personal solution is 12542543681221

shortened code

xinput = "123.328..51.64.
.45.64..387.23.
..6.98..215.314
*...+...*...+..";

With[{xinputStringMatrix =
   Map[Characters, StringSplit[xinput, "\n"]]},
 Total@MapThread[
   Function[{xop, xnumList},
    Apply[xop, xnumList]], {Select[
     ReplaceAll[Last@xinputStringMatrix, {"*" -> Times, "+" -> Plus}],
      Function[x, UnsameQ[".", x]]],
    Map[Function[xrow,
      ToExpression@
       Map[Function[xstr, StringReplace[xstr, "." -> ""]], xrow]],
     Select[SplitBy[StringJoin /@ Transpose@Most@xinputStringMatrix,
       Function[x, StringContainsQ[x, RegularExpression["[0-9]"]]]],
      Function[x, Length@x > 1]]]}]]

(*3263827*)

Advent of Code 2025