Xah Talk Show 2026-01-02 Ep736 Wolfram Language, Advent of Code 2025, Day 6

xah talk show ep736 2130a
xah talk show ep736 2130a

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

solution

Times @@ {3,4}
(* 12 *)

(* the at at is a shortcut syntax for *)
Apply[Times , {3,4}]
(* 12 *)
Total[{3,4}]
(* 7 *)

Apply[Plus , {3,4}]
(* 7 *)
xinput = "123 328  51 64
 45 64  387 23
  6 98  215 314
*   +   *   +";

(* first, turn the text block into a matrix of string*)

xinputStringMatrix =
Map[
 Function[ xline, StringSplit[ xline] ] ,
StringSplit[  xinput, "\n"]
]
(* {{123, 328, 51, 64}, {45, 64, 387, 23}, {6, 98, 215, 314}, {*, +, *, +}} *)

(* extract the last row as list of operator strings *)
xoperatorList = Last @ xinputStringMatrix;

(* now, transpose the top rows, then turn them from string to number *)
xnumberMatrix = ToExpression @ Transpose @ Most @ xinputStringMatrix;

Total @
MapThread[
Function[{numRow, xops},
If[ xops === "+", Apply[Plus , numRow], Apply[Times , numRow] ]
] ,
{xnumberMatrix, xoperatorList}
]
(* 4277556 *)

compact code, one-line

Total @
MapThread[
Function[{numRow, xops},
If[ xops === "+", Apply[Plus , numRow], Apply[Times , numRow] ]
] ,
Function[x, { ToExpression @ Transpose @ Most @ x, Last @ x}] @ Map[ Function[ x, StringSplit @ x ] , StringSplit[ "123 328  51 64
 45 64  387 23
  6 98  215 314
*   +   *   +", "\n"] ]
]
(* 4277556 *)

new algo. shortest code

xinput =
"123 328  51 64
 45 64  387 23
  6 98  215 314
*   +   *   +";
Total @
Map[
Function[x, Apply[ First @ x, Rest @ x]]
,
Transpose @ ToExpression @ RotateRight @ Map[ StringSplit , StringSplit[ StringReplace[ xinput, {"+" -> "Plus", "*" -> "Times"}] , "\n"] ]
]
(* 4277556 *)

Advent of Code 2025